4

ここで storeAreaInCookie をクリックすると、都市の緯度と経度が得られます。これらの結果は、Cookie に保存する必要があり、Web サイトで使用したい場所で使用する必要があります。しかし、このコードを使用しようとすると、 cookie とそのコンソールで、cookie ストアが定義されていないと言っています。この問題を解決するにはどうすればよいですか?

 <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>
4

2 に答える 2

2

追加する参照ファイルが

angular-cookies.js

依存関係エラーの可能性があります

于 2015-02-25T12:54:35.047 に答える
2

angular-cookies の依存関係をいじっている可能性があります。$rootScope の値を変更した後、手動でダイジェスト サイクルを実行する必要があります。$rootScope.$apply()

コード

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();
            });
        });

    };
}]);

働くフィドル

これがあなたを助けることを願っています、ありがとう。

于 2015-02-27T09:39:46.623 に答える