如何解决 angular js 中的 cookieStore is not defined 错误?
How to resolve the cookieStore is not defined error in angular js?
在这里,当我点击 storeAreaInCookie 时,会得到城市的纬度和经度,这些结果我必须存储在 cookie 中,并且需要在我想在我的 website.But 中使用的地方使用使用此代码它不存储 cookie 并在控制台中说 cookie 存储不是 defined.how 我可以解决这个问题吗?
<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>
确保您添加的参考文件
angular-cookies.js
可能是依赖错误
可能是您在搞乱 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();
});
});
};
}]);
希望对您有所帮助,谢谢。
在这里,当我点击 storeAreaInCookie 时,会得到城市的纬度和经度,这些结果我必须存储在 cookie 中,并且需要在我想在我的 website.But 中使用的地方使用使用此代码它不存储 cookie 并在控制台中说 cookie 存储不是 defined.how 我可以解决这个问题吗?
<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>
确保您添加的参考文件
angular-cookies.js
可能是依赖错误
可能是您在搞乱 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();
});
});
};
}]);
希望对您有所帮助,谢谢。