Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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

<div data-ng-repeat="i in [1,2,3,4,5]"> 

 do something
</div>

But if your scope variable includes a range that has a dynamic number then you will need to create an empty array each time.

In the controller

var range = []; 

for(var i=0;i<total;i++) { 

   range.push(i); 

$scope.range = range;

In the HTML

<div data-ng-repeat="i in range">

  do something 

</div>

This works, but it is unnecessary since we won't be using the range array at all within the loop. Does anyone know of setting a range or a regular for min/max value?

Something like:

<div data-ng-repeat="i in 1 .. 100"> 

   do something 

</div>

2 Answers

0 votes
by (106k points)
edited by

You can use the below-mentioned code for looping AngularJS with Numbers & Ranges:-

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; 

      }; 

});

Looking for Angularjs material from basics! Refer to this video on Angularjs provided by Intellipaat:

With the repeat used like this:-

<div ng-repeat="n in [] | range:100"> 

  do something 

</div>

0 votes
ago by (2.8k points)

You can filter defined as follows:

var this_App = angular.module('this_App', []);
this_App.filter('range', function() {

return function(input, total) {
total = parseInt(total);
for (var a=0; a<total; a++)
{
input.push(a);
}
return input;
};
});

When the repeat is used like this:
<div ng-repeat="n in [] | range:100">
do something
</div>

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...