简体   繁体   English

在ng-repeat中使用时的指令范围

[英]Directives scope when used in ng-repeat

I'm struggling to figure out how to create a directive within an "ng-repeat" loop. 我正在努力找出如何在“ ng-repeat”循环内创建指令。

HTML
<article ng-repeat="person in people">
    <div address-count></div>
</article>

JS (doesn't work)
directive('addressCount', function() {
    return {
        template: "Got count " + person.addresses.length
    };    
});

I've tried to figure out how to get the scope set correctly, but everything I've tried has failed. 我试图弄清楚如何正确设置范围,但是我尝试过的一切都失败了。 How can I access "person" within the template of a directive? 如何在指令模板中访问“人”?

You are trying to access person as if it were a global variable. 您正在尝试访问person ,就好像它是全局变量一样。 person is defined on the scope, which can be accessed the same way as in the html: template: "Got count {{person.addresses.length}}" person是在作用域上定义的,可以通过与html中相同的方式进行访问: template: "Got count {{person.addresses.length}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM