javascript - sharing 1 ajax repsonse between controllers -


the big picture - tyring ajax call fire in 1 place , send controllers (there 8) , based on string answer set show/hide in of them. seems pretty simple having trouble working kinks out. able figuire out how shre json object between 2 controllers little bit different i'm thinking maybe i'm applying previous logic worked me in incorrect way.

i set factory share between controllers so

.factory('statuscheck', ['$http', function ($http) {  var currentid = self.location.search; if(currentid[0] === "?"){     currentid = currentid.slice(1); }  var stats = $http({         method: "get",         url: "/getlessondetails/" + currentid     })     .success(function(data, status, headers, config){      return data.status;     });  return stats;  }]) 

all trying here have ajax call (you can ignore currentid part) , return data.status. in each controller inject statuscheck , use it.

once have statuscheck coming right string value (the data.status equal 1 of 3 strings) want use in controller show/hide stuff. have scope boolean set in each controller called $scope.editorenabled toggles between show/hides want (if set true or false.

so once have statuscheck in controller want check value , in controller -

 if(statuscheck == "on"){  $scope.editorenabled = true;  }else if(statuscheck == "off"){   $scope.editorenabled = false;  }else if(statuscheck == "viewonly"){    $scope.editorenabled = false;    $scope.viewonlymode= true;  } 

i don't know if factory working correctly, when want make sure ajax get's string before tries read these if statements in controller, because not wrapped in , fire. don't know if thinking angular, still adjusting myself it's process. suggestions appreciated. reading!

you can set property in factory returning service when http promise resolved , watch property change in controller;

.factory('statuscheck', ['$http', function ($http) {     var me = {         status: 0     };      var currentid = self.location.search;     if(currentid[0] === "?"){       currentid = currentid.slice(1);     }      $http({         method: "get",         url: "/getlessondetails/" + currentid     })     .success(function(data, status, headers, config){        me.status = data.status;     });      return me; }]); 

and in controller, watch statuscheck.status change , whatever want

 $scope.statuscheck = statuscheck;   $scope.$watch('statuscheck.status', function (newvalue) {        if(newvalue == "on"){           $scope.editorenabled = true;        }else if(newvalue == "off"){           $scope.editorenabled = false;        }else if(newvalue == "viewonly"){          $scope.editorenabled = false;          $scope.viewonlymode= true;        }  }); 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -