在 angular js 中使用 cookie 的问题

Issue in using cookie in angular js

我正在尝试使用 cookie(设置和检索),我从网站上复制了这段代码并对其进行了更改,但我无法工作,我的所有 angular 部分都停止工作了。 这是 angular 网站的示例 你能告诉我问题出在哪里吗?

var app = angular.module('test', ['ui.bootstrap'], ['ngCookies']);
app.controller('ExampleController', ['$cookieStore', function ($scope, $cookieStore) {
    // Put cookie
    $cookieStore.put('myFavorite', 'oatmeal');
    // Get cookie
    $scope.itemValue = $cookieStore.get('myFavorite');
    // Removing a cookie
    //$cookieStore.remove('myFavorite');
}]);

用法是:

<span ng-controller="ExampleController">{{itemValue}}</span>

它给我这个错误

Error: [$injector:modulerr] http://errors.angularjs.org/1.3.0-beta.5/$injector/modulerr?......

您的语法不正确,请通过 docs 找到 angular 的正确语法。

你在声明你的模块是错误的,第二个参数应该是一个依赖项数组,但你传递的每个依赖项都是作为它自己单独的数组。应该是:

var app = angular.module('test', ['ui.bootstrap', 'ngCookies']);

您正在为您的控制器使用 "minification safe" 数组,但您只包含 $cookieStore,而不是 $scope,它应该是:

app.controller('ExampleController', ['$scope', '$cookieStore', function ($scope, $cookieStore) {
    ...
}]);