AngularJS错误注入模块

Mic*_*gin 2 javascript angularjs

我正在尝试使用cookie开发登录,检查用户是否已经登录.

按顺序,我包括应用程序:

angular.module('ELOAuthentication', [
    'AuthenticationService',
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])
Run Code Online (Sandbox Code Playgroud)

然后是服务

angular.module('ELOAuthentication').factory('AuthenticationService', function ($rootScope, $scope, $http) {

    var service = {};

    service.Login = function (email, password, callback) {
        var Url = '/api/user/GetLoginByEmailPassword';

        $http.then(Url, { email: email, password: password }).success(
            function (response) {
                var data = response.data
                callback(data);
            }).catch(function (error) {
                console.log('ERROR GetLoginByEmailPassword: ' + error);
            });
    }

    service.SetCookie = function (email, password) {

    };

    service.ClearCookie = function () {

    };
});
Run Code Online (Sandbox Code Playgroud)

最后是AngularJS控制器.

angular.module('ELOAuthentication').controller('LoginController', function ($rootScope, $scope, $http) {

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});
Run Code Online (Sandbox Code Playgroud)

我收到错误:

未捕获的错误:[$ injector:modulerr]

.怎么了?

Zoo*_*oly 5

您不需要在模块中注入服务.

app.js

angular.module('ELOAuthentication', [
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])
Run Code Online (Sandbox Code Playgroud)

LoginController.js

angular.module('ELOAuthentication').controller('LoginController', function ($rootScope, $scope, $http, AuthenticationService) {

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});
Run Code Online (Sandbox Code Playgroud)

正如Sajal所提到的,不要忘记返回您的服务对象:

AuthenticationService.js

angular.module('ELOAuthentication').factory('AuthenticationService', function ($rootScope, $scope, $http) {

    var service = {};

    service.Login = function (email, password, callback) {
        var Url = '/api/user/GetLoginByEmailPassword';

        $http.then(Url, { email: email, password: password }).success(
            function (response) {
                var data = response.data
                callback(data);
            }).catch(function (error) {
                console.log('ERROR GetLoginByEmailPassword: ' + error);
            });
    }

    service.SetCookie = function (email, password) {

    };

    service.ClearCookie = function () {

    };

    return service;
});
Run Code Online (Sandbox Code Playgroud)

官方文件