这是我的 Controller
RegisterController.$inject = ['$location', '$scope', 'Authentication'];
function RegisterController($location, $scope, Authentication){
var vm = this;
vm.register = register;
function register(){
Authentication.register(vm.email, vm.password, vm.confirm_password, vm.username);
}
function register_error_display(error_list){
console.log("what is going on")
}
}
这是我的服务
function Authentication($cookies, $http, RegisterController){
var Authentication = {
register: register
};
return Authentication;
// Register logic
function register(email, password, confirm_password, username){
return $http.post('/api/v1/accounts/',{
username: username,
password: password,
confirm_password: confirm_password,
email: email
}).then(registerSuccess, registerError);
}
function registerError(response){
error_list = response["data"];
RegisterController.register_error_display(error_list);
}
}
流程是这样的register( Controller )
-> post(服务)
-> registerError(服务)
-> register_error_display ( Controller )
我相信这会返回此错误
Error: [$injector:unpr] Unknown provider: RegisterControllerProvider <- RegisterController <- Authentication
这个错误的原因是什么,有解决办法吗?
最佳答案
您无法在服务中注入(inject) Controller 。原因之一是每次创建指令时都会实例化 Controller ,而服务则在启动时实例化一次。
相反,您可以使 Authentication.register 函数返回一个 promise 。可以使用 $q 服务创建一个。
或者,如果您不想在身份验证服务中身份验证成功或失败后执行任何操作,只需返回从 $http.post 调用中获得的 promise 即可。
返回http.post响应:
服务 函数身份验证($cookies,$http){
var Authentication = {
register: register
};
return Authentication;
// Register logic
function register(email, password, confirm_password, username){
return $http.post('/api/v1/accounts/',{
username: username,
password: password,
confirm_password: confirm_password,
email: email
});
}
Controller
RegisterController.$inject = ['$location', '$scope', 'Authentication'];
function RegisterController($location, $scope, Authentication){
var vm = this;
vm.register = register;
function register(){
Authentication.register(vm.email, vm.password, vm.confirm_password, vm.username).then(onSuccess, onError);
}
function onSuccess(result) {
console.log("HTTP request succeeded");
}
function onError(result) {
console.log("HTTP request failed");
}
}
你自己的 promise : 服务
function Authentication($cookies, $http, $q){
var Authentication = {
register: register
};
return Authentication;
// Register logic
function register(email, password, confirm_password, username){
var deferred = $q.defer();
$http.post('/api/v1/accounts/',{
username: username,
password: password,
confirm_password: confirm_password,
email: email
}).then(function(result) {
deferred.resolve(result);
}, function(error) {
deferred.reject(result);
});
return deferred.promise();
}
}
Controller
RegisterController.$inject = ['$location', '$scope', 'Authentication'];
function RegisterController($location, $scope, Authentication){
var vm = this;
vm.register = register;
function register(){
Authentication.register(vm.email, vm.password, vm.confirm_password, vm.username).then(onSuccess, onError);
}
function onSuccess(result) {
console.log("Authentication success");
}
function onError(result) {
console.log("Authentication success");
}
}
我建议您做出并返回自己的 promise 。这样您就可以从 Controller 中抽象出身份验证的实际发生方式。 例如,您仍然可以有一个成功的请求,但响应可能是身份验证失败。
关于javascript - Angular Service 可以调用 Controller 中调用该服务的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164416/