admin管理员组

文章数量:1429844

Directive code

mymodule.directive('eicon', function(){
    return {
        restrict: 'E',
        scope: {
            attr: '='
        },
        template: "test " + attr.name
    }
});

Html

<tr ng-repeat="e in entities">
    <td><eicon attr="e"></eicon></td>
</tr>

I've this error: ReferenceError: attr is not defined. What's wrong?

Directive code

mymodule.directive('eicon', function(){
    return {
        restrict: 'E',
        scope: {
            attr: '='
        },
        template: "test " + attr.name
    }
});

Html

<tr ng-repeat="e in entities">
    <td><eicon attr="e"></eicon></td>
</tr>

I've this error: ReferenceError: attr is not defined. What's wrong?

Share Improve this question asked Feb 8, 2015 at 12:50 brazorfbrazorf 1,9612 gold badges32 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Since you are declaring isolated scope property attr you should be able to access scope.attr in template like this:

mymodule.directive('eicon', function(){
    return {
        restrict: 'E',
        scope: {
            attr: '='
        },
        template: "test {{attr.name}}"
    }
});

Demo: http://plnkr.co/edit/YZ0aPqhkMlIIwmrkKK2v?p=preview

attr is accessible in scope, so you can access scope.attr in your controller or linking phase, or {{attr}} in templates. A simple solution is to change your template to

mymodule.directive('eicon', function(){
    return {
        restrict: 'E',
        scope: {
            attr: '='
        },
        template: "test {{attr.name}}",
        link: function (scope, element, attrs) {
          console.log(scope.attr);
        },
        controller: function (scope) {
          console.log(scope.attr);
        }
    }
});

本文标签: javascriptAngularJS directive scope not resolved (quotattr name is not definedquot error)Stack Overflow