1

I have a module and two controllers :

var module = angular.module("app", ["agGrid", "ngAnimate", "ngSanitize", "ngDialog"])


module.controller("mainCtrl", ["$scope", "dataService","$timeout","dateFilter","ngDialog", "$http", function ($scope, $http, $timeout, dateFilter, ngDialog, dataService) {
}]);


module.controller("modalCtrl", ["$scope", "ngDialog", "dataService", function ($scope, ngDialog, dataService) {


$scope.printEntity = function () {
    console.log(dataService.getEntityArray());
}

}]);

And a service:

 module.factory("dataService", function () {

var entityArrayService = [];

return {
    setEntityArray: function (entityArray) {
        entityArrayService = entityArray;
    },
    getEntityArray: function () {
        return entityArrayService;
    }

};

});

I can call dataService.setEntityArray(array) from inside my SECOND controller, but when i try to call this from my first controller it tells me dataService.setEntityArray is not a function

2 Answers 2

2

The order of dependency injections is incorrect. Arguments in the controller function must repeat the order of the elements in the array. In your case dataService is the second argument:

module.controller("mainCtrl", ["$scope", "dataService","$timeout","dateFilter","ngDialog", "$http", function ($scope, dataService, $timeout, dateFilter, ngDialog, $http) {
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

You are kidding me, the order matters!? Thanks! will accept when time runs out.
Of course :) Thats the point of array notation so that Angular knows what argument corresponds to what service. So that after minification when it's ['dataService', '$http', function(a, b) {}] Angular knows that a is DataService, etc.
Oh I see! Thanks for your help!
1

You have invalid order of the variables in the first controller definition. It should be:

function ($scope, dataService, $timeout, dateFilter, ngDialog, $http)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.