AngularJS For Loop with Numbers & Ranges

后端 未结 24 3059
抹茶落季
抹茶落季 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:26

    A short way of doing this would be to use Underscore.js's _.range() method. :)

    http://underscorejs.org/#range

    // declare in your controller or wrap _.range in a function that returns a dynamic range.
    var range = _.range(1, 11);
    
    // val will be each number in the array not the index.
    <div ng-repeat='val in range'>
        {{ $index }}: {{ val }}
    </div>
    
    0 讨论(0)
  • 2020-11-22 14:26

    Without any change in your controller, you can use this:

    ng-repeat="_ in ((_ = []) && (_.length=51) && _) track by $index"
    

    Enjoy!

    0 讨论(0)
  • 2020-11-22 14:27
    <div ng-init="avatars = [{id : 0}]; flag = true ">
      <div ng-repeat='data in avatars' ng-if="avatars.length < 10 || flag"
           ng-init="avatars.length != 10 ? avatars.push({id : $index+1}) : ''; flag = avatars.length <= 10 ? true : false">
        <img ng-src="http://actual-names.com/wp-content/uploads/2016/01/sanskrit-baby-girl-names-400x275.jpg">
      </div>
    </div>
    

    If you want to achieve this in html without any controller or factory.

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

    Method definition

    The code below defines a method range() available to the entire scope of your application MyApp. Its behaviour is very similar to the Python range() method.

    angular.module('MyApp').run(['$rootScope', function($rootScope) {
        $rootScope.range = function(min, max, step) {
            // parameters validation for method overloading
            if (max == undefined) {
                max = min;
                min = 0;
            }
            step = Math.abs(step) || 1;
            if (min > max) {
                step = -step;
            }
            // building the array
            var output = [];
            for (var value=min; value<max; value+=step) {
                output.push(value);
            }
            // returning the generated array
            return output;
        };
    }]);
    

    Usage

    With one parameter:

    <span ng-repeat="i in range(3)">{{ i }}, </span>
    

    0, 1, 2,

    With two parameters:

    <span ng-repeat="i in range(1, 5)">{{ i }}, </span>
    

    1, 2, 3, 4,

    With three parameters:

    <span ng-repeat="i in range(-2, .7, .5)">{{ i }}, </span>
    

    -2, -1.5, -1, -0.5, 0, 0.5,

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

    Set Scope in controller

    var range = [];
    for(var i=20;i<=70;i++) {
      range.push(i);
    }
    $scope.driverAges = range;
    

    Set Repeat in Html Template File

    <select type="text" class="form-control" name="driver_age" id="driver_age">
         <option ng-repeat="age in driverAges" value="{{age}}">{{age}}</option>
    </select>
    
    0 讨论(0)
  • 2020-11-22 14:31

    Suppose $scope.refernceurl is an array then

    for(var i=0; i<$scope.refernceurl.length; i++){
        $scope.urls+=$scope.refernceurl[i].link+",";
    }
    
    0 讨论(0)
提交回复
热议问题