AngularJS指令范围没有解决(QUOT; attr指示名未被定义"错误) [英] AngularJS directive scope not resolved ("attr name is not defined" error)

查看:126
本文介绍了AngularJS指令范围没有解决(QUOT; attr指示名未被定义"错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指令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>

我有此错误:的ReferenceError:attr为没有定义。怎么了?

推荐答案

ATTR 是范围访问,以便您可以访问 scope.attr 在模板控制器或链接阶段,或 {{} ATTR} 。一个简单的解决方案是将您的模板更改为

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);
        }
    }
});

这篇关于AngularJS指令范围没有解决(QUOT; attr指示名未被定义&QUOT;错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆