gpt4 book ai didi

javascript - AngularJS 错误注入(inject)模块

转载 作者:行者123 更新时间:2023-11-29 16:39:48 25 4
gpt4 key购买 nike

我正在尝试开发一个带有 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 Controller 。

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

AuthenticationService.ClearCookie();

$scope.init = function () {

}

$scope.login = function () {

};
});

我收到错误:

Uncaught Error: [$injector:modulerr]

。怎么了?

最佳答案

您不需要在模块中注入(inject)服务。

app.js

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

LoginController.js

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

AuthenticationService.ClearCookie();

$scope.init = function () {

}

$scope.login = function () {

};
});

正如 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;
});

Official documentation

关于javascript - AngularJS 错误注入(inject)模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47633996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com