繁体   English   中英

Angular JS-模板中的ngRepeat

[英]Angular JS - ngRepeat in a Template

我正在尝试在指令中使用ngRepeat-但我真的不知道该怎么做。 我靠近吗?

割草机是控制器中使用此指令的数组。

.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
        }
    }
});

是的,您很接近,但是语法已关闭。 并且您需要为示波器上的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 }
            ];
        }
    }
});

另外,您不必本身引用<slider>

上面的代码还假设您已经为<film><frame>了指令。

最后,如果要从外部范围传递割草机数组,则可以将范围设置更改为:

scope: {
   mowers: '='
}

然后您可以像这样设置它:

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

其中myMowers是控制器或父指令范围内的变量。

希望对您有所帮助。

暂无
暂无

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

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