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?
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.
I think it would be better not attaching underscore to angular but use it directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With