1

我尝试在自定义服务中使用角度 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*/
            }
        }
    }
})();
4

1 回答 1

1

ngCookies是模块不是依赖名称,您应该注入ngCookies模块依赖并用于$cookies获取 cookie 对象

//somewhere in app.js
angular.module('app', ['otherModules', ..... , 'ngCookies'])

还要在$inject 数组中添加$cookies缺少的依赖项。checkLoginService

angular.module('app')
.service('checkLoginService', ['$cookies', checkLoginService]);
checkLoginService.$inject = ['$http', '$cookies'];
function checkLoginService($http, $cookies) {
    return {
        checkLogin: function () {
            /*logic*/
        }
    }
}
于 2016-04-27T15:22:41.067 回答