1

I have a component that I want to pass a variable to.

Basically my component is called by:

<profileimage></profileimage>

I want to pass it an ID by

<profileimage userid="user.ID"></profileimage>

I would be able to then use that ID in the controller to load an image

function ProfileImageController($scope) {
   var ctrl = this;
} 
var app = angular.module('creatif')
    .component('profileimage', {
       templateUrl: 'components/profileimage/profileimage.html',
       controller: ProfileImageController
   });
4
  • What's stopping you? Commented Jun 16, 2017 at 5:09
  • @31piy I am unsure how to access userid? Commented Jun 16, 2017 at 5:10
  • Include your component's definition in the question. Commented Jun 16, 2017 at 5:10
  • @31piy have updated the description Commented Jun 16, 2017 at 5:14

1 Answer 1

1

Use bindings to receive the variables in your custom component.

.component('profileimage', {
   templateUrl: 'components/profileimage/profileimage.html',
   controller: ProfileImageController,
   bindings: {
     userid: '<'
   }
});

Then you can use userid in your controller by this.userid. Go through the angular's documentation to know more.

Bonus Tip:

Move your directive definition out of the controller function.

function ProfileImageController($scope) {
   var ctrl = this;
}

angular.module('creatif')
  .component('profileimage', {
     templateUrl: 'components/profileimage/profileimage.html',
     controller: ProfileImageController,
     bindings: {
       userid: '<'
     }
  });

Looks more readable, no?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.