这是我的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")
}
}
这是我的service
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 (service)
- > registerError (service)
- > register_error_display (controller)
我相信这会给我带来这个错误
Error: [$injector:unpr] Unknown provider: RegisterControllerProvider <- RegisterController <- Authentication
这个错误的原因是什么,是否有办法解决?
答案 0 :(得分:0)
您无法在服务中注入控制器。其中一个原因是每次创建指令时都会实例化控制器,而a服务在启动时实例化一次。
相反,您可以使 Authentication.register 函数返回一个承诺。可以使用 $ q 服务创建一个。
或者,如果您在身份验证服务成功或失败后不想执行任何操作,只需返回您从 $ http.post 调用中获得的承诺。
返回 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
});
}
控制器
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");
}
}
你自己的承诺: 服务
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();
}
}
控制器
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");
}
}
我建议制作并回复你自己的承诺。这样,您就可以从控制器中抽象出实际进行身份验证的方式。 例如,您仍然可以获得成功的请求,但响应可能是身份验证失败。