Localstorage is working for first time. If I refresh page at second it is working fine

495 Views Asked by At

I am using AngularJS local storage. I am facing an issue with local storage. At first when page is loaded the localstorage is assigning with null value. If I refresh at second time localstorage value is setting fine. I don't know what the issue.

My code is.

        var app = angular.module('EntityApp', ['ngRoute', 'slickCarousel']);
    app.controller('EntityAppCntroller', function($scope, $http, $window) {
        if (localStorage.getItem('IsNewPinSet') != 1) {
            var getLocation = function() {
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(showPosition);
                } else {
                    alert("Geolocation is not supported by this browser.");
                }
            };
            getLocation();

            function showPosition(position) {
                var latitude;
                var longitude;
                latitude = position.coords.latitude;
                longitude = position.coords.longitude;
                localStorage.setItem('latitudeHome', latitude)
                localStorage.setItem('longitudeHome', longitude)
                var geocoder = new google.maps.Geocoder();
                var latlng = new google.maps.LatLng(latitude, longitude);
                geocoder.geocode({
                    'latLng': latlng
                }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        if (results[0]) {
                            for (var i = 0; i < results[0].address_components.length; i++) {
                                var types = results[0].address_components[i].types;
                                for (var typeIdx = 0; typeIdx < types.length; typeIdx++) {
                                    if (types[typeIdx] == 'postal_code') {
                                        var pin_all = results[0].address_components[i].short_name;
                                        localStorage.setItem('pincodehome', pin_all)
                                        $scope.pin_all = pin_all;
                                    }
                                }
                            }
                        } else {
                            console.log("No results found");
                        }
                    }
                });
            };
        }
        // $window.localStorage.getItem('pincodehome') is null
        // $window.localStorage.getItem('latitudeHome') is null
        // $window.localStorage.getItem('longitudeHome') is null
        //Common response in home page
        $http.get('someurl?pincode=' + localStorage.getItem('pincodehome') + '&lat=' + localStorage.getItem('latitudeHome') + '&lang=' + localStorage.getItem('longitudeHome'), {}).success(function(response) {
            $scope.Promotion = response.Promotion.Response;
        });
    });
0

There are 0 best solutions below