在这里,当我单击 storeAreaInCookie 这将导致城市的纬度和经度时,我必须将这些结果存储在 cookie 中,并且需要在我想在我的网站中使用的任何地方使用。但是当我尝试使用此代码时,它不会存储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>