4

Here when i click on storeAreaInCookie that will result the lat and long of the city those results i have to store in cookies and need to use where ever i want to use in my website.But when am tried to use this code it not storing the cookie and in it console saying that cookie store is not defined.how can i resolve this problem?

 <div ng-app="MyApp" ng-controller="MyCtrl">
    <div ng-repeat="x in names">
     <div ng-click="storeAreaInCookie(x.city)" >{{x.city}}</div>
    </div>
    </div>
    <script>
    angular.module('MyApp', ['ngCookies'])
    .controller('MyCtrl', ['$scope', '$http',"$rootScope", "$cookieStore", function($scope, $http, $rootScope, $cookieStore) {
    $scope.storeAreaInCookie = function (area) {
            var geocoder =  new google.maps.Geocoder();
            var lat="";
            var long="";
            geocoder.geocode( { 'address': area}, function(results, status) {
                 if (status == google.maps.GeocoderStatus.OK) {
                        $rootScope.lat=results[0].geometry.location.lat();
                        $rootScope.long=results[0].geometry.location.lng();
                    } else {
                       alert("Something went wrong " + status);
                     }
                codeLatLng($rootScope.lat,$rootScope.long);
                 $(document).ready(function(){
                    $("#locationView").slideToggle();
                });
             });
          $cookieStore.put('lat',$rootScope.lat);
          $cookieStore.put('long',$rootScope.long);   
          alert("lat"+$cookieStore.get('lat'));
          alert("long"+$cookieStore.get('long'));
          };
    }]);
    </script>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Sujithrao
  • 789
  • 3
  • 12
  • 27
  • any error in console? – Pankaj Parkar Feb 26 '15 at 14:01
  • @pankaj yes its showing that cookiestore is not defined .could you please help me out to resolve this problem – Sujithrao Feb 27 '15 at 09:21
  • check my answer you code is working fine only i mocked the value and skipped google.map – Pankaj Parkar Feb 27 '15 at 09:40
  • @pankaj here i will give the city as input to geocoder and i will get the latitude and longitude from geocoder then i will store the results in cookie.Here i am getting the problem with geocoder it is executing after the cookie store so there am getting lat long as empty first the empty. – Sujithrao Feb 27 '15 at 10:01
  • you need to put those line of code($cookieStore related code) inside the success of geocode, because it is running using promise..like – Pankaj Parkar Feb 27 '15 at 10:14
  • @pankaj if i put inside success of geocode am getting a error in console that is cookieStore is not defined and cookies are not storing .Why this error is coming i have tried many ways but the problem couldnt reosolved yet. – Sujithrao Feb 27 '15 at 10:32
  • can you please create a fiddle/plunkr of this example? – Pankaj Parkar Feb 27 '15 at 10:39
  • check updated code and fiddle..it is working..please do upvote if it is helpful – Pankaj Parkar Feb 27 '15 at 10:59

2 Answers2

2

Make sure that your adding reference file

angular-cookies.js

its may be dependency error

  • yes i have added angular-cookies.js and angularjs-1.2.26.js cdn in my page still am getting same problem cookie value not storing. – Sujithrao Feb 25 '15 at 12:58
2

May be you are messing with angular-cookies dependency. And after changing value in $rootScope you need to run digest cycle manually using $rootScope.$apply()

CODE

angular.module('MyApp', ['ngCookies'])
    .controller('MyCtrl', ['$scope', '$http', "$rootScope", "$cookieStore", "$q", function ($scope, $http, $rootScope, $cookieStore, $q) {
    $scope.names = [{
        "id": 1,
        city: "abc",
        lat: 123,
        long: 345
    }, {
        "id": 2,
        city: "asd",
        lat: 123,
        long: 345
    }, {
        "id": 3,
        city: "dfg",
        lat: 123,
        long: 345
    }];
    //mock ajax
    $scope.storeAreaInCookie = function (area) {
        var geocoder = new google.maps.Geocoder();
        var lat = "";
        var long = "";
        geocoder.geocode({
            'address': area.city
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                $rootScope.lat = results[0].geometry.location.lat();
                $rootScope.long = results[0].geometry.location.lng();
                $rootScope.$apply()
                $cookieStore.put('lat', $rootScope.lat);
                $cookieStore.put('long', $rootScope.long);
                alert("lat" + $cookieStore.get('lat'));
                alert("long" + $cookieStore.get('long'));
            } else {
                alert("Something went wrong " + status);
            }
            //codeLatLng($rootScope.lat, $rootScope.long);
            $(document).ready(function () {
                $("#locationView").slideToggle();
            });
        });

    };
}]);

Working Fiddle

Hope this could help you, Thanks.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • could you please help me out to solve this problem i have posted here .http://stackoverflow.com/questions/28761516/when-a-radio-button-is-in-ng-repeat-when-i-click-on-radio-button-the-div-will-be – Sujithrao Feb 27 '15 at 11:17
  • @Sujithrao check i gave answered to it – Pankaj Parkar Feb 27 '15 at 11:32