javascript - Asynchronous global variables in AngularJS -



javascript - Asynchronous global variables in AngularJS -

i'm using angularjs , need have global several global objects can accessed controller in app.

for illustration 1 object need user object has users id , other properties. user object comes database via ajax. need way set user object, initialize controllers used on page. giving app page load fist load of program.

then if object isn't set, need redirect.

does have thought of how cleanly? i've been trying utilize broadcast truing spaghetti code.

currently utilize ui-router, , have hidden view controller globalsctrl. globalsctrl uses service objects , $broadcasts them controllers can initialize. but.... broadcast works on initial site load. when changing $location.paths event not broadcast because globalsctrl variables set.

i add together if statements seems messy me.

please help. thanks!

plunker - http://plnkr.co/edit/tizdxoxpdv3d7pt5ah8i

var app = angular.module('editor.services', []); app.factory('analysisdataservice', ['$http', function($http) { var self = this; self.activeanalysis = {}; self.openanalysis = function(analysisid) { homecoming $http.get('api/v1/assignments/analysis/' + analysisid) .success(function(data, status, headers, config) { self.activeanalysis = data; homecoming self.activeanalysis; }).error(function(data, status, headers, config) { console.log("error not load analysis article."); }).then(function(result) { homecoming self.activeanalysis; }); }; self.getanalysis = function() { homecoming self.activeanalysis; }; self.navigatetostep = function(step) { $location.path('/analysis/summary'); }; homecoming { open: self.openanalysis, get: self.getanalysis, navigate: self.navigatetostep, } }]);

the problem need self.activeanalysis variable set before few other controllers load. each page loads different data-set based on analysisid.

ui-router has resolve method can utilize in routes. resolve tell routing wait , resolve before moves new route.

here examples(your problem similar authentication):

angular ui-router login authentication https://gist.github.com/leon/6550951

javascript ajax angularjs global

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -