[ACCEPTED]-How to make lodash work with Angular JS?-lodash
I prefer to introduce '_' globally and injectable 1 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._;
});
I just wanted to add some clarification 13 to @beret's and @wires's answer. They definitely 12 helped and got the jist of it but getting 11 the whole process in order might suit someone. This 10 is how I set up my angular environment with 9 lodash and got it working with yeoman gulp-angular 8 to serve properly
bower install lodash --save
(This adds to bower 7 and a saves to bower json)modify bower json 6 to have lodash load before angular does. (This 5 helps if you're using gulp inject and don't 4 want to manually put it into index.html. otherwise 3 put it into the index.html before the angular 2 link)
Make a new constant per @wires's direction.
'use strict'; angular.module('stackSample') // lodash support .constant('_', window._);
- Inject into any angular service, filter, or controller as you would anything else:
.filter('coffeeFilter', ['_', function(_) {...}]);
- To throw it into some angular html view just inject it into the controller and assign a scope variable to it:
.controller('SnesController', function ($scope, _) { $scope._ = _; })
Hope 1 this helps someone set up their site. :)
ng-repeat
requires an Angular expression, which has 3 access to Angular scope variables. So instead 2 assigning _
to this
, assign it to the $scope
object 1 you inject into the controller:
IndexModule.controller('GridController', function ($scope) {
$scope._ = _;
})
I am not sure what version of Angular you 5 are using. Looks like you should have just 4 used the 'Controller as' syntax when you 3 use 'this' to access variables in the dom.
Here 2 is the solution with this and not using 1 scope. http://plnkr.co/edit/9IybWRrBhlgQAOdAc6fs?p=info
<body ng-app="myApp" ng-controller="GridController as grid">
<div ng-repeat="n in grid._.range(5)">
<div>Hello {{n}}</div>
</div>
</body>
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.