I have an ajax call inside the .run() that loads a variable into the $rootScope That variable is needed in the controller associated with a view. Sometimes on refresh (F5) by the time the .controller is loading there is nothing inside $rootScope.SuperCategories.
sampleApp.factory('SuperCategoryService', ['$http', '$q', '$rootScope', function ($http, $q, $rootScope){
var SuperCategoryService = {};
SuperCategoryService.SuperCategories = $rootScope.SuperCategories;
alert ($rootScope.SuperCategories);
return SuperCategoryService;
}]);
sampleApp.run(function($rootScope, $q, $http) {
var req = {
method: 'POST',
url: 'SuperCategoryData.txt',
//url: 'http://localhost/cgi-bin/superCategory.pl',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }//,
//data: { action: 'GET' }
};
$rootScope.SuperCategories = [];
$rootScope.GetSuperCategories = function () {
var defer = $q.defer();
$http(screq).then(function(response) {
$rootScope.SuperCategories = response.data;
//alert ($rootScope.SuperCategories);
defer.resolve($rootScope.SuperCategories);
}, function(error) {
defer.reject("Some error");
});
return defer.promise;
}
$rootScope.GetSuperCategories();
});
How to fix this bug.
undefinedor[]? If the later, that's because your promise is not resolved yet.