angularjs - Controller not loading value from factory after ui-router state change -


i have list of icons on webpage generated object array. when user clicks on icon, corresponding object array passed function in factory saves name of object selected, $state.go called change routes. on new route controller loaded loads same factory , tries access name of saved object. problem 7 times out of 10, works perfectly, , other 3 times gives "unable property 'name' of undefined or null reference" type error.

here controller passing selected value factory:

platformhome.controller('platformhome', ['$scope', 'appmanager', '$state',  function ($scope, appmanager, $state) {      var sf = appmanager.state.sf;     var = appmanager.state.so;      $scope.productlineselected = function (product) {        setproductline(product);                          };      function setproductline(product) {         sf.setproduct(product);         $state.go('metricdashboard');            }  }]); 

here factory:

applicationmanager.factory('appstatemanager', ['$rootscope', '$sessionstorage', '$state', function ($rootscope, $sessionstorage, $state) {  //    state object classes // var stateclasses = {};  stateclasses.productline = function (name) {     this.name = name;     this.dashboard = {         mode: 'reporting', //reporting, analysis         modeview: 'canvas', //canvas, data         index: {             report: 0,             userreport: 0,             canvas: 0,             group: 0,             element: 0,             filter: 0,         }     };     this.reports = [];     this.canvases = [new stateclasses.canvas]; };   //    state data functions // var statefunctions = {};  statefunctions.setproduct = function (product) {     session.stateobject.productline.current = product.code;     session.stateobject[product.code] = (typeof session.stateobject[product.code] === 'undefined') ? new stateclasses.productline(product.name) : session.stateobject[product.code]; };   //    stucture // var statescope = $rootscope.$new(true);  var session = $sessionstorage; session.stateobject = (typeof session.stateobject === 'undefined') ? new stateclasses.stateobject : session.stateobject;  statescope.so = session.stateobject; statescope.sf = statefunctions;  return statescope;  }]); 

here controller trying access name:

metricdashboard.controller('metricdashboard', ['$scope', 'appmanager', function ($scope, appmanager) {      var sf = appmanager.state.sf;     var = appmanager.state.so;      dso = so[so.productline.current];     $scope.name = dso.name;  }]); 

i suspect issue related order in things happening, however, cannot figure out why works 7 times out of 10.

when error, have been able determine line so.productline.current in second controller has value of none, meaning doesn't seem have been updated scope of controller, however, @ same time, i'm using console.log(json.stringify()) inside factory, , factory indeed show proper value instead of none.

i've tried using $timeout on $state.go, , tried passing callback, neither of prevent issue. again 7 times out of 10, code runs fine , name property value, not.

i able correct problem few steps. idea remove assignment of dso variable metricdashboard controller , move functionality of assignment factory, reference newly assigned variable in controller.

here changes:

in factory

...  //    state data functions // var statefunctions = {};  statefunctions.setproduct = function (product) {     session.stateobject.productline.current = product.code;     session.stateobject[product.code] = (typeof session.stateobject[product.code] === 'undefined') ? new stateclasses.productline(product.name) : session.stateobject[product.code];          //new functionality assignment         session.dynamicstateobject = session.stateobject[product.code];         statescope.dso = session.dynamicstateobject; };   //    stucture // var statescope = $rootscope.$new(true);  var session = $sessionstorage; session.stateobject = (typeof session.stateobject === 'undefined') ? new stateclasses.stateobject : session.stateobject;      //new structure persist assignment beyond page refresh     session.dynamicstateobject = (typeof session.dynamicstateobject === 'undefined') ? {} : session.dynamicstateobject;      //new reference     statescope.dso = session.dynamicstateobject;  statescope.so = session.stateobject; statescope.sf = statefunctions;  return statescope; 

in controller

var sf = appmanager.state.sf; var = appmanager.state.so;  //removed assignmnet //dso = so[so.productline.current];  //added reference var dso = appmanager.state.dso;  $scope.name = dso.name; 

while have not yet tested new code extensively, have not been able reproduce error.


Comments