原来我不知道ngCookies是一个单独的服务,它不是标准angular.js的一部分。我的bower.json文件引用角度为1.4beta但角度cookie仍然加载1.3。我将其改为1.4beta,一切都按预期工作。
我在Angular 1.4项目的控制器中使用$ cookies但是当我引用$ cookies时出现以下错误:
TypeError: undefined is not a function
at Object.<anonymous> (http://localhost:9000/bower_components/angular-cookies/angular-cookies.js:60:16)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4371:17)
at Object.enforcedReturnValue [as $get] (http://localhost:9000/bower_components/angular/angular.js:4224:37)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4371:17)
at http://localhost:9000/bower_components/angular/angular.js:4189:37
at getService (http://localhost:9000/bower_components/angular/angular.js:4330:39)
at invoke (http://localhost:9000/bower_components/angular/angular.js:4362:13)
at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:4379:27)
at http://localhost:9000/bower_components/angular/angular.js:8668:28
at link (http://localhost:9000/bower_components/angular-route/angular-route.js:981:26) <div ng-view="" class="ng-scope">
我的代码:
angular.module('supportalApp', [
'ngCookies',
'ngAnimate',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch',
'cfp.hotkeys',
'ngClipboard']);
angular.module('supportalApp')
.controller('ToolsCtrl', function($scope, $cookies) {....
});
如果我删除$ cookies,则不会出现任何错误。只是为了确认,我已经包含了&#39; ngCookie&#39;在应用程序
答案 0 :(得分:3)
我没有在模块依赖项中添加'ngCookies'
,请添加它。
angular.module('supportalApp', ['ngCookies'])
.controller('ToolsCtrl', function($scope, $cookies) {....
});
答案 1 :(得分:3)
请注意,$ cookies API在AngularJS v1.4.0-beta.6之前曾经是一个关联数组,而在线文档(默认为开发快照)已经引用了新的get / put API。相关更改为https://github.com/angular/angular.js/commit/38fbe3ee8370fc449b82d80df07b5c2ed2cd5fbe
使用AngularJS&lt; = v1.4.0-beta.5,您有以下选择: