我正在尝试在控制器中以下列方式导入ngCookies。我在添加ngCookies时收到以下错误。
参数'HomeCtrl'不是函数,未定义
Controller.js:
angular.module('angularRestfulAuth', ['ngCookies']);
angular.module('angularRestfulAuth')
.controller('HomeCtrl', ['$rootScope', '$scope', '$location','$cookies', '$localStorage', 'Main', function($rootScope, $scope, $location,$cookies, $localStorage, Main) {
这是我的服务,
angular.module('angularRestfulAuth')
.factory('Main', ['$http','$cookies', '$localStorage', function($http,$cookies, $localStorage){
我的索引html有,
<body ng-app="angularRestfulAuth">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation" data-ng-controller="HomeCtrl">
....
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-cookies.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-local-storage/0.6.0/angular-local-storage.min.js"></script>
答案 0 :(得分:1)
ngCookies 没有任何问题,但如果您想使用Angular Local Storage,则需要将其注入模块:
angular.module('angularRestfulAuth', ['LocalStorageModule']);
您可以在控制器或工厂中使用此服务:
angular.module('angularRestfulAuth')
.controller('HomeCtrl', ['localStorageService', function(localStorageService) {
localStorageService.get(key);
...
}
angular.module('angularRestfulAuth')
.factory('Main', ['localStorageService', function(localStorageService){
localStorageService.get(key);
...
}
但我认为你不应该同时使用 ngCookies 和 AngularLocalStorage ,因为它们都是针对相同的东西设计的,使用浏览器cookie(并且还使用 AngularLocalStorage 的本地存储空间
Here是使用带 AngularLocalStorage
的Cookie的示例