Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

underscore _.range() not work at AngularJS ng-repeat

If I want only 20 iteration, how can I repeat my block? It isnt work:

<div ng-repeat="item in _.range(20)"></div>

UnderscoreJS included in the page

like image 503
Paul Kononenko Avatar asked Feb 18 '13 16:02

Paul Kononenko


1 Answers

If you want to use undersore's functions in your template you will have to expose it on a scope. If you want to have it available in all templates one way of doing so would be:

var app = angular.module('angularjs-starter', []);

app.run(function($rootScope){
  $rootScope._ = _;
});

Then you could use it in a template as you've tried:

<div ng-repeat="item in _.range(20)">{{item}}</div>

Here is a working plunk: http://plnkr.co/edit/1Va4EikvRyFiQvhb2HYV?p=preview

While the above works it shouldn't be used. Model should be initialized in a controller. Otherwise AngularJS will execute _range on each $digest cycle to generate a new array.

like image 114
pkozlowski.opensource Avatar answered Oct 19 '22 20:10

pkozlowski.opensource