角度ng重复支持从0到n重复,而不调用控制器

Does angular ng repeat support repeat from 0 to n, without calling controller?

本文关键字:重复 控制器 调用 ng 支持 角度      更新时间:2024-01-08

Javascript-

angular
  .module('app.somecontroller', [])
  .controller('SomeController', SomeController)
function SomeController(){
  containerInd = [0,1,2,3,4]
}

HTML-

<ul>
  <li ng-repeat='i in containerInd'>
    {{i}}
  </li>
</ul>

据我所知,ng repeat必须使用控制器本身上的对象,像一样重复一遍怎么样

for(var i=0;i<n;i++)
<li ng-repeat='i to 5'>

angular ng能重复吗?

--编辑--

啊,我明白了我可以做一些类似的事情

<li ng-repeat='(key,value) in containerList'

索里:(,我以前的问题不清楚我在containerList上有字符串列表,需要在ng repeat 中获取列表的键

是的,你可以,但你必须找到一种方法将它附加到$scope,在你的情况下是$rootScope

var app = angular.module('plunker', []);
app.run(function($rootScope){
  $rootScope.list = [0,1,2,3,4];
})

示例

Simply use Filters:-
    <li ng-repeat="n in [] | range:10">
    {{n}}
    </li>
var myApp = angular.module('myApp', []);
myApp.filter('range', function() {
  return function(input, total) {
    total = parseInt(total);
    for (var i=0; i<total; i++)
      input.push(i);
    return input;
  };
});

Fiddle