How To Use Underscore In Your AngularJS Controllers

Posted by Weston Ganger

I wanted to be able to use underscore in my controllers.


Make sure you include underscore.js on the page


First we will create a service or factory so we can inject it


var underscore = angular.module('underscore', []);
underscore.factory('_', ['$window', function() {
  return $window._;
}]);


Now we can use it like so:


// Declare it as a dependency of your module
var app = angular.module('app', ['underscore']);

// And then inject it on the controllers where you need it
app.controller('ListCtrl', function($scope, _) {
  // do stuff with underscore
});


Related External Links:

Article Topic:Software Development - Angular

Date:July 21, 2015

Recommended Posts