Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make lodash work with Angular JS?

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?

like image 253
diegoaguilar Avatar asked May 26 '14 03:05

diegoaguilar


People also ask

Can I use lodash in angular?

We can easily use Lodash in Angular. Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc. Lodash library can be broken down into several categories.

Can we use lodash in TypeScript?

Consuming. From there you'll be able to use lodash in your TypeScript code with no fuss. This works for both modules and global code.

Can we use lodash in JavaScript?

Lodash is an extremely popular JavaScript library that provides a lot of useful functions for working with strings, arrays and objects in your web projects. Some of the Lodash functions are now supported natively in modern JavaScript, but the library still adds value and saves you time.


2 Answers

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._;   }); 
like image 133
wires Avatar answered Sep 28 '22 06:09

wires


I just wanted to add some clarification to @beret's and @wires's answer. They definitely helped and got the jist of it but getting the whole process in order might suit someone. This is how I set up my angular environment with lodash and got it working with yeoman gulp-angular to serve properly

  • bower install lodash --save (This adds to bower and a saves to bower json)

  • modify bower json to have lodash load before angular does. (This helps if you're using gulp inject and don't want to manually put it into index.html. otherwise put it into the index.html before the angular 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 this helps someone set up their site. :)

like image 28
ThinkBonobo Avatar answered Sep 28 '22 07:09

ThinkBonobo