I have inherited the angular application and should now make a change.
As part of this change, some data must be installed in one controller, and then used from another. Therefore, I created a service and one controller wrote data to it, and one controller read data from it.
angular.module('appRoot.controllers') .controller('pageController', function (myApiService, myService) { // load data from API call var data = myApiService.getData(); // Write data into service myService.addData(data); }) .controller('pageSubController', function (myService) { // Read data from service var data = myService.getData(); // Do something with data.... })
However, when I use data in pageSubController , it is always undefined.
How can I make sure pageController runs before pageSubController ? Or is this even the right question?
EDIT
My service code:
angular.module('appRoot.factories') .factory('myService', function () { var data = []; var addData = function (d) { data = d; }; var getData = function () { return data; }; return { addData: addData, getData: getData }; })
source share