AngularJS For Loop with Numbers & Ranges

后端 未结 24 3113
抹茶落季
抹茶落季 2020-11-22 13:40

Angular does provide some support for a for loop using numbers within its HTML directives:

do something <
相关标签:
24条回答
  • 2020-11-22 14:41

    I tweaked this answer a bit and came up with this fiddle.

    Filter defined as:

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

    With the repeat used like this:

    <div ng-repeat="n in [] | range:100">
      do something
    </div>
    
    0 讨论(0)
  • 2020-11-22 14:41

    I came up with a slightly different syntax which suits me a little bit more and adds an optional lower bound as well:

    myApp.filter('makeRange', function() {
            return function(input) {
                var lowBound, highBound;
                switch (input.length) {
                case 1:
                    lowBound = 0;
                    highBound = parseInt(input[0]) - 1;
                    break;
                case 2:
                    lowBound = parseInt(input[0]);
                    highBound = parseInt(input[1]);
                    break;
                default:
                    return input;
                }
                var result = [];
                for (var i = lowBound; i <= highBound; i++)
                    result.push(i);
                return result;
            };
        });
    

    which you can use as

    <div ng-repeat="n in [10] | makeRange">Do something 0..9: {{n}}</div>
    

    or

    <div ng-repeat="n in [20, 29] | makeRange">Do something 20..29: {{n}}</div>
    
    0 讨论(0)
  • 2020-11-22 14:41

    For those new to angularjs. The index can be gotten by using $index.

    For example:

    <div ng-repeat="n in [] | range:10">
        do something number {{$index}}
    </div>
    

    Which will, when you're using Gloopy's handy filter, print:
    do something number 0
    do something number 1
    do something number 2
    do something number 3
    do something number 4
    do something number 5
    do something number 6
    do something number 7
    do something number 8
    do something number 9

    0 讨论(0)
  • 2020-11-22 14:41

    This is the simplest variant: just use array of integers....

     <li ng-repeat="n in [1,2,3,4,5]">test {{n}}</li>

    0 讨论(0)
  • 2020-11-22 14:43

    I use my custom ng-repeat-range directive:

    /**
     * Ng-Repeat implementation working with number ranges.
     *
     * @author Umed Khudoiberdiev
     */
    angular.module('commonsMain').directive('ngRepeatRange', ['$compile', function ($compile) {
        return {
            replace: true,
            scope: { from: '=', to: '=', step: '=' },
    
            link: function (scope, element, attrs) {
    
                // returns an array with the range of numbers
                // you can use _.range instead if you use underscore
                function range(from, to, step) {
                    var array = [];
                    while (from + step <= to)
                        array[array.length] = from += step;
    
                    return array;
                }
    
                // prepare range options
                var from = scope.from || 0;
                var step = scope.step || 1;
                var to   = scope.to || attrs.ngRepeatRange;
    
                // get range of numbers, convert to the string and add ng-repeat
                var rangeString = range(from, to + 1, step).join(',');
                angular.element(element).attr('ng-repeat', 'n in [' + rangeString + ']');
                angular.element(element).removeAttr('ng-repeat-range');
    
                $compile(element)(scope);
            }
        };
    }]);
    

    and html code is

    <div ng-repeat-range from="0" to="20" step="5">
        Hello 4 times!
    </div>
    

    or simply

    <div ng-repeat-range from="5" to="10">
        Hello 5 times!
    </div>
    

    or even simply

    <div ng-repeat-range to="3">
        Hello 3 times!
    </div>
    

    or just

    <div ng-repeat-range="7">
        Hello 7 times!
    </div>
    
    0 讨论(0)
  • 2020-11-22 14:43

    Simplest no code solution was to init an array with the range, and use the $index + however much I want to offset by:

    <select ng-init="(_Array = []).length = 5;">
        <option ng-repeat="i in _Array track by $index">{{$index+5}}</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题