2015-08-23 79 views
1

這裏是我的controllerCan Angular Service可以在調用服務的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 

什麼是這個錯誤的原因,是有辦法解決它?

+1

錯誤很明顯:你試圖注入未知服務,沒有服務'RegisterController'(它不是服務)。 – dfsq

+0

是否有可能將控制器注入服務?如果我想在服務器中的控制器中使用某個功能,有什麼替代方案? – Kevin

回答

0

您不能在服務中注入控制器。其中一個原因是每次創建一個指令時都會實例化一個控制器,而在啓動時一個服務被實例化。

取而代之,您可以使Authentication.register函數返回一個承諾。可以使用$ q服務創建一個。

或者,如果您在驗證成功或驗證服務失敗後不想執行任何操作,只需返回您從$ http.post調用中獲得的承諾。

返還http.post響應:

服務 功能驗證($餅乾,$ 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"); 
    } 

}

我建議製作和返回自己的諾言。這樣你就可以從控制器中抽象出真正的身份驗證過程。 例如,您仍然可以獲得成功的請求,但響應可能是身份驗證失敗。

相關問題