0

I have declared my app and have one controller (for demonstration purposes):

    var module = angular.module("app", [])


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


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

}]);

And a service:

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

var entityArrayService = [1,2];

return {

    getEntityArray: function () {
        return entityArrayService;
    }

};

});

When I call $scope.printEntity from my view, I'm always told dataService.getEntityArray() is undefined.

I've loaded the service as a dependency and declared my entityArrayService array outside of my return statement. I've looked high and low for an answer but to no avail. My goal is to share a piece of data between two controllers, but at the minute I can't even use one controller to retrieve data.

4 Answers 4

1

The service isn't loaded as a dependency. Change this line:

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

to this:

module.controller("modalCtrl", ["$scope", "dataService", function ($scope, dataService) {
Sign up to request clarification or add additional context in comments.

Comments

0

You are using strict syntax for dependencies declaration. So if you add a parameter to your controller, you must add its declaration too.

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

Comments

0

You didn't inject dataService in your controller. Try with:

module.controller("modalCtrl", ["$scope", "dataService", function ($scope, dataService) {
  // ...
});

Comments

0

The injection of the service in the controller is missing:

please correct to:

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

The other code is correct.

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.