Way to ng-repeat defined number of times instead of repeating over array?

前端 未结 26 3228
刺人心
刺人心 2020-11-22 14:53

Is there a way to ng-repeat a defined number of times instead of always having to iterate over an array?

For example, below I want the list item to show

相关标签:
26条回答
  • 2020-11-22 15:39

    simple way:

        public defaultCompetences: Array<number> = [1, 2, 3];
    

    in the component/controller and then:

        <div ng-repeat="i in $ctrl.defaultCompetences track by $index">
    

    This code is from my typescript project but could be rearranged to pure javascript

    0 讨论(0)
  • 2020-11-22 15:44

    I wanted to keep my html very minimal, so defined a small filter that creates the array [0,1,2,...] as others have done:

    angular.module('awesomeApp')
      .filter('range', function(){
        return function(n) {
          var res = [];
          for (var i = 0; i < n; i++) {
            res.push(i);
          }
          return res;
        };
      });
    

    After that, on the view is possible to use like this:

    <ul>
      <li ng-repeat="i in 5 | range">
        {{i+1}} <!-- the array will range from 0 to 4 -->
      </li>
    </ul>
    
    0 讨论(0)
提交回复
热议问题