Iamisti
Iamisti

Reputation: 1710

Angular scope property not accessible from directive in attribute

I have a controller like this:

var exampleController = function($scope, $stateParams){
    $scope.myVariable = $stateParams.id;
    console.log($scope.myVariable);
}

and a directive like:

var exampleDirective = function(){
    return {
        restrict: 'E',
        scope : {
            myVariable: '='
        },
        templateUrl: "myTemplate.html",
        link: function($scope){
               console.log($scope.myVariable);
        });
     };
}

in my html like:

<my-example myVariable="myVariable"></my-example>

Given the id in the url is 21, I got this:

21
undefiend

Do anybody know why I couldn't pass any $stateParams value so that the directive don't see it?

I've tried to pass a static value but it didn't worked too. Tried in this way within the controller:

$scope.myVariable = 26;

Upvotes: 0

Views: 588

Answers (1)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

You forgot the Angular convention for attribute names:

<my-example my-variable="myVariable"></my-example>

Upvotes: 1

Related Questions