Angularjs

How To Use Underscore In Your AngularJS Controllers

Posted by Weston Ganger on July 21, 2015

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:


Posted in Angular and Tagged with angularjs underscore 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts