Without seeing how everything is being implemented. This is the best I can help you with. If you want a controller to do something only if a promise is successful you can wrap your code around the request. In the plunkr I have written a sample $http service that has a fake request to myFunction that uses $q.
I would suggest using a factory to share data between controller instead of $rootScope. $rootScope is hard to manage throughout big SPA's. The Plunkr has commented options you can mess with to change between $rootScope and using a Factory.
Service below
app.service('Service', Service); function Service($q, $rootScope, Factory) { var deferred = $q.defer(); this.myFunction = function(){ //Using factory to persit data instead of $rootScope //var itm = Factory.myArray; var itm = $rootScope.var; var array = []; //Item isnt set return error if(itm === undefined || itm === null) deferred.reject("$rootScope.var is not set") //Changed this a bit didnt know what $rootScope.var actually was for (var i in itm) { array.push(itm[i]); } deferred.resolve(array); return deferred.promise; } return this; }
The first thing the controller does is initializes a request to Service.myFunction() and waits for a success or error callback. After the success you can process and do anything you'd like with the data returned from the promise. If there is an error you can handle it as you see fit.
app.controller('controller', controller); function controller(Service, $rootScope) { /* jshint validthis: true */ var vm = this; vm.myArray = []; vm.request = ""; //Un-Comment this to return success or error $rootScope.var = [1,2,3,4,5,6]; //This is a fake http request Service.myFunction().then( //if the promise was resolved or $http was a success //initilize the controller function(data) { vm.myArray = (data) }, //if the promise was resolved or $http was a success //initilize the controller function(err) { vm.request = (err) }) }
Plunkr
$scopehere. A plunkr/codepen would definitely help debug what you're seeing as it is more than likely a logic glitch. Also, if you are ever doing something with a scope that you want to propagate, make sure you never usesetTimeoutor pure callbacks without using$scope.apply().bind(). So you could do something likefunction(){}.bind($scope)and then within function usethis.myArrayinstead.$timeoutinstead ofsetTimeout.