我正在尝试在Angular中使用Cookie - 这就是我正在尝试的内容:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular-cookies.min.js"></script>
var capApp = angular.module('capApp', ['ngRoute','ui.bootstrap','ngCookies']);
capApp.controller('cookieCtrl', ['$scope','$cookies', function($scope, $cookies) {
var favoriteCookie = $cookies.get('user_id');
alert(favoriteCookie);
}]);
我在控制台中收到此错误:
TypeError: $cookies.get is not a function
我出错的任何想法?
更新
检查你正在使用哪个版本的Angular - 任何Angular人都会读到这个,让文档中的版本切换为鲜绿色和巨大!你根本就没注意到它。
答案 0 :(得分:34)
答案 1 :(得分:11)
The syntax you have is for version 1.4.x but you are referencing an older version. If you do not want to update to 1.4.x, you can use $cookieStore instead as follows:
O(n^2)
Alternatively if you are using version 1.3.x or below, you can use $cookies as follows:
$cookieStore.put("key", "value");
var value = $cookieStore.get("key");
答案 2 :(得分:2)
以下方法应该有效。
var favoriteCookie = $cookies.user_id;