Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching underscore or lodash to angular

Is it ok to attach underscore.js variable to angular variable? so I can call underscore like: angular._ ? Since underscore is less likely to be mocked at testing and we can't declare global variables?

if so, which part of my angular.js application should I add it?

like image 840
Louie Almeda Avatar asked Jul 09 '15 08:07

Louie Almeda


2 Answers

I prefer to create a wrapper service in it's own injectable module like such:

angular.module('underscore.service', [])
.factory('_', function () {
  return window._; // assumes underscore has already been loaded on the page
});

As noted, you should include underscore.js before angular in your html as you typically would.

This approach allows makes underscore accessible in a testing environment.

like image 115
user1464581 Avatar answered Nov 14 '22 23:11

user1464581


I think it would be better not attaching underscore to angular but use it directly.

like image 32
atinder Avatar answered Nov 14 '22 23:11

atinder