AngularJS错误注入模块

时间:2017-12-04 12:59:18

标签: javascript angularjs

我尝试使用Cookie开发登录,检查用户是否已经登录。

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

angular.module('ELOAuthentication', [
    'AuthenticationService',
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])

然后服务

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 () {

    };
});

最后是AngularJS控制器。

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

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});

我收到错误:

  

未捕捉错误:[$ injector:modulerr]

。有什么问题?

1 个答案:

答案 0 :(得分:5)

您无需在模块中注入服务。

app.js

EmployeeVO

LoginController.js

angular.module('ELOAuthentication', [
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])

如Sajal所述,请不要忘记返回您的服务对象:

AuthenticationService.js

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

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});

Official documentation