在ng重复中使用时的指令范围

Directives scope when used in ng-repeat

本文关键字:指令 范围 ng      更新时间:2023-09-26

我很难弄清楚如何在"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
    };    
});

我试图弄清楚如何正确设置作用域,但我所尝试的一切都失败了。如何访问指令模板中的"人员"?

您正试图访问person,就好像它是一个全局变量一样。person是在作用域上定义的,可以通过与html中相同的方式访问:template: "Got count {{person.addresses.length}}"