我尝试在自定义服务中使用角度Cookie,但收到错误: 未知提供者:ngCookiesProvider< - ngCookies< - checkLoginService
我将模块,控制器和服务存储在单独的文件中。
控制器:
(function() {
'use strict';
angular
.module('app')
.controller('AuthController', AuthController);
AuthController.$inject = ['$scope', '$http', '$location', 'checkLoginService'];
function AuthController($scope, $http, $location, checkLoginService) {
/* jshint validthis:true */
var vm = this;
vm.title = 'AuthController';
$scope.login = function(user) {
/*logic*/
}
$scope.checklogin = function () {
if (checkLoginService.checkLogin()) {
/*logic*/
}
}
$scope.checklogin();
}
})();
服务:
(function () {
'use strict';
angular
.module('app')
.service('checkLoginService', ['ngCookies', checkLoginService]);
checkLoginService.$inject = ['$http'];
function checkLoginService($http, $cookies) {
return {
checkLogin: function () {
/*logic*/
}
}
}
})();
答案 0 :(得分:1)
ngCookies
模块不是依赖项名称,您应该在模块依赖项中注入$cookies
并使用//somewhere in app.js
angular.module('app', ['otherModules', ..... , 'ngCookies'])
来获取cookie对象
$cookies
还在checkLoginService
$ inject数组中添加angular.module('app')
.service('checkLoginService', ['$cookies', checkLoginService]);
checkLoginService.$inject = ['$http', '$cookies'];
function checkLoginService($http, $cookies) {
return {
checkLogin: function () {
/*logic*/
}
}
}
缺少的依赖项。
eig