1

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

1
  • 2
    don't need both factory and constant ... use one or the other Commented Feb 4, 2016 at 21:22

1 Answer 1

3

You're missing the _ reference in your controller declaration I think? Try this:

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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Also not sure you need to rely on _ being declared by dependency injection if you are registering it as a constant throughout the app. I could be wrong though.
That was it. And you are correct I removed the constant and just left it as a service.

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.