Use underscore inside Angular controllers

JavascriptAngularjsunderscore.js

Javascript Problem Overview


How do I use underscore library inside angularjs controllers?

On this post: https://stackoverflow.com/questions/11943130/angularjs-limitto-by-last-2-records somebody suggested to assign an _ variable to the rootScope so that the library will be available to all the scopes within the app.

But I'm not clear where to do it. I mean should it go on the app module declaration? i.e:

var myapp = angular.module('offersApp', [])
            .config(['$rootScope', function($rootScope) { }

But then where do I load underscore lib? I just have on my index page the ng-app directive and script reference to both the angular-js and underscore libs?

index.html:

<head>
</head>
<body ng-app="offersApp">
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="scripts/vendor/angular.js"></script>
<script src="scripts/vendor/underscore.js"></script>
...  

How do I achieve this?

Javascript Solutions


Solution 1 - Javascript

When you include Underscore, it attaches itself to the window object, and so is available globally.

So you can use it from Angular code as-is.

You can also wrap it up in a service or a factory, if you'd like it to be injected:

var underscore = angular.module('underscore', []);
underscore.factory('_', ['$window', function($window) {
  return $window._; // assumes underscore has already been loaded on the page
}]);

And then you can ask for the _ in your app's module:

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

// And then inject it where you need it
app.controller('Ctrl', function($scope, _) {
  // do stuff
});

Solution 2 - Javascript

I have implemented @satchmorun's suggestion here: https://github.com/andresesfm/angular-underscore-module

To use it:

  1. Make sure you have included underscore.js in your project

     <script src="bower_components/underscore/underscore.js">
    
  2. Get it:

     bower install angular-underscore-module
    
  3. Add angular-underscore-module.js to your main file (index.html)

     <script src="bower_components/angular-underscore-module/angular-underscore-module.js"></script>
    
  4. Add the module as a dependency in your App definition

     var myapp = angular.module('MyApp', ['underscore'])
    
  5. To use, add as an injected dependency to your Controller/Service and it is ready to use

     angular.module('MyApp').controller('MyCtrl', function ($scope, _) {
     ...
     //Use underscore
     _.each(...);
     ...
    

Solution 3 - Javascript

I use this:

var myapp = angular.module('myApp', [])
  // allow DI for use in controllers, unit tests
  .constant('_', window._)
  // use in views, ng-repeat="x in _.range(3)"
  .run(function ($rootScope) {
     $rootScope._ = window._;
  });

See https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection about halfway for some more info on run.

Solution 4 - Javascript

You can also take a look at this module for angular

https://github.com/floydsoft/angular-underscore

Solution 5 - Javascript

If you don't mind using lodash try out https://github.com/rockabox/ng-lodash it wraps lodash completely so it is the only dependency and you don't need to load any other script files such as lodash.

Lodash is completely off of the window scope and no "hoping" that it's been loaded prior to your module.

Solution 6 - Javascript

you can use this module -> https://github.com/jiahut/ng.lodash

this is for lodash so does underscore

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPabloView Question on Stackoverflow
Solution 1 - JavascriptsatchmorunView Answer on Stackoverflow
Solution 2 - JavascriptunifyView Answer on Stackoverflow
Solution 3 - JavascriptwiresView Answer on Stackoverflow
Solution 4 - JavascriptPaul SheldrakeView Answer on Stackoverflow
Solution 5 - JavascriptNick WhiteView Answer on Stackoverflow
Solution 6 - JavascriptjiahutView Answer on Stackoverflow