Attaching underscore or lodash to angular

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.