Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  144] [ 7]  / answers: 1 / hits: 71050  / 10 Years ago, mon, may 26, 2014, 12:00:00

I'm trying to use lodash use it at ng-repeat directives, in this way:



<div ng-controller=GridController ng-repeat=n in _.range(5)>
<div>Hello {{n}}</div>
</div>


Being GridController:



IndexModule.controller('GridController', function () {

this._ = _

})


However is not working and so, nothing being repeated. If I change the directive to ng-repeat=i in [1,2,3,4,5] it'll work.



lodash is already included via <script> at <header> before angular. How can I make it work?


More From » angularjs

 Answers
29

I prefer to introduce '_' globally and injectable for tests, see my answer to this question
Use underscore inside controllers



var myapp = angular.module('myApp', [])
// allow DI for use in controllers, unit tests
.constant('_', window._)
// use in views, ng-repeat=x in _.range(3)
.run(function ($rootScope) {
$rootScope._ = window._;
});

[#70852] Friday, May 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;