我正在做一个简单的"添加到购物车"系统。我希望我的篮子中的物品不会消失,所以我试图使用angularjs饼干。它可以在不添加cookie项目的情况下工作,添加和删除ok。
var app = angular.module('myApp', ['ui.bootstrap','ngCookies']);
app.controller('menusCtrl', ['$cookies',function($scope, $http,$cookies) {
$http.get("getContent.php").success(function (response) {$scope.items = response.menus;});
$scope.basketlist = [];
$scope.updateCart = function (index) {
var item = $scope.items[index];
$scope.basketlist.push({quantity: 1, price: item.menu_price,no:item.menu_no,image:item.menu_img});
$cookies.put("basketlist",$scope.basketlist);
}
$scope.remove = function(index) {
var itemtodelete = $scope.basketlist.indexOf(index);
$scope.basketlist.splice(index, 1);
}
}]);
我插入/angularjs/1.4.2/和/1.4.2/angular-cookies.js
我收到了这些错误:
TypeError: Cannot read property 'get' of undefined
at new <anonymous> (app.js:4)
at Object.e [as invoke] (angular.js:4452)
at $get.P.instance (angular.js:9001)
at S (angular.js:8111)
at g (angular.js:7543)
at g (angular.js:7547)
at angular.js:7418
at angular.js:1635
at n.$get.n.$eval (angular.js:15848)
at n.$get.n.$apply (angular.js:15947)(anonymous function) @ angular.js:12332$get @ angular.js:9111$get.n.$apply @ angular.js:15949(anonymous function) @ angular.js:1633e @ angular.js:4452d @ angular.js:1631Ac @ angular.js:1651Zd @ angular.js:1545(anonymous function) @ angular.js:28361m.Callbacks.j @ jquery.js:3148m.Callbacks.k.fireWith @ jquery.js:3260m.extend.ready @ jquery.js:3472J @ jquery.js:3503
答案 0 :(得分:0)
$scope.mybasket = JSON.parse($cookies.get('mybasket'));
$cookies.put('mybasket',JSON.stringify($scope.mybasket));
我使用JSON.stringify和JSON.parse并且它有效。