AngularJS 指令范围未解析(“属性名称未定义"错误) [英] AngularJS directive scope not resolved ("attr name is not defined" error)

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

问题描述

指令代码

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 is not defined.怎么了?

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

推荐答案

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 指令范围未解析(“属性名称未定义"错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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