0

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...

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.