vendredi 29 janvier 2016

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

I have a api data,fetching it and showing it in website using angular js. i am just trying to store the api data in local storage other than fetching data from api everytime. so i coded like 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 the problem is,It is taking the data from api, not from the local storage. if i comment code of getting response from api. it will fetch from the local storage. if i mention api also along with the local, it will take the from api. i've checked in chrome developer tools. i can't use else condition too for this. can anyone tell me is there any way to do this.

Aucun commentaire:

Enregistrer un commentaire