我正在尝试创建一个身份验证模块,当我尝试注入我的身份验证服务时,它无法加载该模块。
这是我的代码 - 的 AuthenticationController.js
define(['app',"../services/AuthenticationService"], function (app) {
app.controller('AuthController',["$scope","AuthService", function ($scope, AuthService) {
$scope.login = function () {
var promise = AuthService.login($scope.email, $scope.password);
promise.then(function (result) {
alert("successful!");
console.log(result);
}).catch(function (error) {
console.log(error);
});
}
}]);
});
AuthenticationService.js
define(['app'], function (app) {
app.factory('AuthService', function ($http) {
var AuthService = {
login: function (email, password) {
var url = "http://localhost:3000/auth/login";
var def = $q.defer();
$http({
method: 'POST',
url: url,
data: {
"email": email,
"password": password
}
}).then(function (data) {
if (data.data.success) {
def.resolve();
} else {
def.reject(data.data.error);
}
}, function (error) {
def.reject(error);
});
return def.promise;
}
};
return AuthService;
});
});
我得到的错误是 -
Error: ng:areq Bad Argument Argument 'AuthController' is not aNaNunction, got undefined
App.js
define(['angularAMD', 'angular-route'], function (angularAMD) {
var app = angular.module("amazonfresh", ['ngRoute']);
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when("/index", angularAMD.route({
templateUrl: '/partials/login.html', controller: 'AuthController', controllerUrl: './controllers/AuthController'
}))
.otherwise({redirectTo: "/index"});
$locationProvider.html5Mode(true);
});
return angularAMD.bootstrap(app);
});
当我从AuthService
中移除AuthController
的依赖关系时,代码可以正常运行。