AngularJS For Loop with Numbers & Ranges

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

    Usage

    With one parameter:

    {{ i }}, 
    

    0, 1, 2,

    With two parameters:

    {{ i }}, 
    

    1, 2, 3, 4,

    With three parameters:

    {{ i }}, 
    

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

提交回复
热议问题