简体   繁体   English

Angular JS-模板中的ngRepeat

[英]Angular JS - ngRepeat in a Template

I'm trying to use ngRepeat in a directive - but I really don't know how to do it. 我正在尝试在指令中使用ngRepeat-但我真的不知道该怎么做。 Am I close? 我靠近吗?

Mowers is an array in my controller where this directive is used. 割草机是控制器中使用此指令的数组。

.directive('slider', function() {
return function() {
    template: '<slider><film><frame ng-repeat="mower in mowers">{{mower.series}}</frame></film></slider>';
    replace: true;
        scope: true;
    link: function postLink(scope, iElement, iAttrs) {
            // jquery to animate
        }
    }
});

Yes, you're close, but the syntax was off. 是的,您很接近,但是语法已关闭。 and you need to assign something to mowers on your scope. 并且您需要为示波器上的mowers分配一些东西。

.directive('slider', function() {
return {
    restrict: 'E', //to an element.
    template: '<film><frame ng-repeat="mower in mowers">{{mower.series}}</frame></film>',
    replace: true,
    scope: true, // creates a new child scope that prototypically inherits
    link: function postLink(scope, iElement, iAttrs) {
            scope.mowers = [
               { series: 1 },
               { series: 2 },
               { series: 3 }
            ];
        }
    }
});

Also, you need to not reference <slider> in itself. 另外,您不必本身引用<slider>

The above code also assumes you've made directives for <film> and <frame> . 上面的代码还假设您已经为<film><frame>了指令。

Finally, if you wanted to pass the mowers array in from an external scope, you'd change your scope setting to this: 最后,如果要从外部范围传递割草机数组,则可以将范围设置更改为:

scope: {
   mowers: '='
}

And you could then set it like so: 然后您可以像这样设置它:

<slider mowers="myMowers"></slider>

where myMowers is a variable on your controller or parent directive's scope. 其中myMowers是控制器或父指令范围内的变量。

I hope that helps. 希望对您有所帮助。

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

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