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 

Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts