angularjs - Is there a way to set priority for the if condition rather than the next lines of code? -


i have api data, fetching , showing in website using angularjs. trying store api data in local storage rather fetching data api everytime. coded this:

       if($window.localstorage.cityapi){             $scope.cities = json.parse($window.localstorage.cityapi);         }         addcityservice.init(function(city_response) {             $scope.loading = false;             $scope.cities = city_response.data;             $scope.city = json.parse(json.stringify(getcity($stateparams._id)));             $window.localstorage.cityapi = json.stringify($scope.cities);         }); 

but problem is, taking data api, not local storage. if comment code of getting response api. fetch local storage. if mention api along local, take data api. i've checked in chrome developer tools. can't use else condition this.because have pages add city , show added cities. if add city once, redirect page show added cities. so, if use else here, not move else statement itself, hence not show added city. can tell me there way this.

how possible can't use else statement here?

if($window.localstorage.cityapi){     $scope.cities = json.parse($window.localstorage.cityapi); } else {     addcityservice.init(function(city_response) {         $scope.loading = false;         $scope.cities = city_response.data;         $scope.city = json.parse(json.stringify(getcity($stateparams._id)));         $window.localstorage.cityapi = json.stringify($scope.cities); }); 

does not work?


Comments