I've started playing around with loddash and I have a small angular app that I am trying to get to work. I have seen similar SO questions around this and underscorejs. But looking at the answers I have not been able to get it to load.
Within my app.js file I create a factory to register the '_' to window._
var app = angular.module("app", [])
.constant('_', window._);
app.factory('_', ['$window', function ($window) {
return $window._;
}]);
Within my html I have lodash loading before the angular libraries
<script src="scripts/lodash.js"></script>
<script src="scripts/angular.js"></script>
<script src="scripts/angular-loader.js"></script>
<script src="scripts/angular-animate.js"></script>
<script src="app/app.js"></script>
<script src="app/treeController.js"></script>
<script src="app/treeviewDirective.js"></script>
<script src="app/test.js"></script>
And then within the controller I tried to inject the '_' service/factory
app.controller("treeController", ['$scope', function($scope, _) {
var vm = this;
function nestAssociation(node, oldCollection, newAggregates) {
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
var selectedNode = node.parent;
var itemIndex = _.findIndex(oldCollection.children, function (o) { return o.parent == selectedNode; });
console.log(itemIndex);
}
The page builds and initializes fine where I get an error is within the function nestAssociation(). As soon as the first lodash function is called _.concat I get an error TypeError: Cannot read property 'concat' of undefined which indicates that the app cannot access the lodash function which I assume is because the service is not registering the '_' correctly.
Can someone tell me what I am missing?
Thanks in advance