Scipion
Scipion

Reputation: 11888

Using lodash with Angularjs

I have found an app online using AngularJS with Lodash. The way Lodash is included is simply by adding to the body the following line (after having included angular):

<script src='vendor/lodash/3.3.1/lodash.min.js'></script>
<script src='myApp.js'></script>

Inside myApp.js, the first line is :

/* global angular, _ */

And then you have access to Lodash (using _) I am not sure to understand why it works...

Upvotes: 0

Views: 3199

Answers (2)

Aj1
Aj1

Reputation: 973

I would include lodash the following way.

Include it in index.html before angular, just like You did (although I prefer using bower command,which would make it available globally). And Have constant in your module

angular.module('sampleApp', [])
    .constant('_', _);

Inject it into your controller or angular component. Hope this helps.

.controller('sampleAppController', function ($scope) {
  $scope._ = _;
})

Also, You can attach it to rootScope, I wouldn't prefer doing it though.

Upvotes: 1

markusthoemmes
markusthoemmes

Reputation: 3120

Lodash includes itself on the global scope level. It does so by attaching _ to the window object (see code here). That comment line you're seeing has nothing to do with this. It's a configuration line for a linter like JSHint, so it doesn't throw errors at you because it thinks those variables are undefined.

Upvotes: 2

Related Questions