簡體   English   中英

在angularjs 1.4.2中使用cookie

[英]using cookies in angularjs 1.4.2

我正在做一個簡單的“添加到購物車”系統。 我希望購物籃中的物品不消失,所以我嘗試使用angularjs cookie。 它無需添加Cookie項目,添加和刪除即可。

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
$scope.mybasket = JSON.parse($cookies.get('mybasket'));
$cookies.put('mybasket',JSON.stringify($scope.mybasket));

我使用JSON.stringify和JSON.parse,它的工作原理。

將$ 作用域和$ http添加到控制器的參數中,如下所示:

app.controller('menusCtrl', ['$scope', '$http','$cookies',function($scope, $http,$cookies) {
...
}])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM