angular promise - AngularJs controller value doesn't update after factory resolve -


i'm trying build user page shows it's main data, details, address, etc.. when make first load, doesn't show on screen until change page , go user page. it's problaby because of promise return after went page.

i can't (don't want to) use data loaded during resolve on statechange because make app feels freezing, since change screen after data resolved, , if take time, seems page isn't working.

i tried using $scope.$digest() (on controller) , $rootscope.$apply() on factory, both situations give me digest running error.

using .then() isn't option (unless there proper way) because if i'm returning page after first load, there errors.

the way i'm loading parent (abstract) controller , storing values in factory, this:

//parent controller function parentcontroller(requestfactory, storefactory, $q) {     var         user = storefactory.userget(),         data = storefactory.dataget()         [...more load...];      if(!user || !data ...) {         var promises = {             user:    requestfactory.get('userget'),             data:    requestfactory.get('dataget'),             [...more requests...]         };          return $q.all(promises).then(function(resolve){             storefactory.useradd(resolve.user);             storefactory.dataadd(resolve.data);             [...more update...]         });     } }  //factory function storefactory() {     var          data    = null,         user    = null,         address = null,         endereco = null;      var service = {         userget: _userget,         useradd: _useradd,         [...more definitions...]     };     return service;       function _userget() {         return user;     }     function _useradd(data) {         return user = data;     }     [...more functions...] }  // child controller 01 function child1controller(storefactory) {     vm.user = storefactory.userget(); }  // child controller 02 function child2controller(storefactory) {     vm.address = storefactory.addressget(); } 

i need load these data on beginning because there dependent data, example, address depends on user, etc..


so, if access page user > address it's not showing on screen, if go other page , came back, show it's supposed show.

is there way solve problem? make data update automatically?

the way solve problem, create single object nested arrays in factory, instead of creating multiple arrays. also, in controller, can't set specific array direct vm, need set array inside object, way it's automatically updated.

//factory var data = {     user: null,     address: null,     [...] };  function _useradd(value) {     return data.user = value; } [...etc...]  //controller function child1controller(storefactory) {     vm.maindata = storefactory.data; } 

after doing this, need specify nested array on view, this:

<p>{{vm.maindata.user}}</p>  <ul>     <li ng-repeat="address in vm.maindata.address track $index">{{address}}</li> </ul> 

even though it's working, doesn't feel right way do, so, if there better way achieve result, i'm open suggestions.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -