在angularjs指令范围内暴露一个对象,无法访问属性
发布时间:2020-12-17 07:49:34 所属栏目:安全 来源:网络整理
导读:所以,我有以下比较简单的Angularjs指令 app.directive('myDirective',function () { return { restrict: 'E',scope: { site: '@',index: '@' },template: 'div{{site}}/div',replace: true,} }); 这里是我在HTML中调用该指令的地方 div id="eventGraphic" cl
所以,我有以下比较简单的Angularjs指令
app.directive('myDirective',function () { return { restrict: 'E',scope: { site: '@',index: '@' },template: '<div>{{site}}</div>',replace: true,} }); 这里是我在HTML中调用该指令的地方 <div id="eventGraphic" class="span12"> <my-directive ng-repeat="site in IEvent.sites" site="{{site}}" index="{{$index}}"></my-directive> </div> 其中,给定每个站点是一个对象,生成此输出(从浏览器复制)
您需要使用’=’来映射对象. ‘@’意味着你只是传递一个字符串值到新的范围.
app.directive('myDirective',scope: { site: '=',//two-way binding index: '@' //just passing an attribute as a string. },} }); 然后在标记中,不要在属性中使用绑定,只需传递表达式: <div id="eventGraphic" class="span12"> <!-- below,site="site" is passing the expression (site) to the two way binding for your directive's scope,whereas index="{{$index}}" is actually evaluating the expression ($index) and passing it as a string to the index attribute,which is being put directly into the directive's scope as a string --> <my-directive ng-repeat="site in IEvent.sites" site="site" index="{{$index}}"></my-directive> </div> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |