2017-01-02 133 views
-1

我想從角度服務回調,但回調函數不起作用。http post回調不工作角工廠

這裏是我的控制器代碼: -

$scope.uu = "hello" // this i just make for test 
$scope.login = function(user){ 
    //console.log(user) 
    AuthenticationService.Login(user.username,user.password).success(function(data){ 
     console.log(data); 
     $scope.uu = data; 
    }) 
} 

我的服務代碼是: -

mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){ 
    var service = {}; 
    service.Login = function (username, password, callback) { 

      $http({ 
     method: "POST", 
     //headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     url: 'users/authenticate', 
     data: {email: username,password:password} 
    }) 
    .success(callback); 
     }; 
     return service; 
}); 

無論是我得到控制檯響應。範圍都沒有改變。 我已經提到這個問題,但答案不適合我。 $http POST response from service to controller

+0

檢查在調用API時發生的錯誤 –

+0

我從api獲得正確的響應。 api正在迴應。 –

+0

只有回調不起作用,即使我寫console.log('test'),那在控制器中也不起作用 –

回答

1

如果你正在編寫服務代碼應該是這樣的:

mainApp.service('AuthenticationService',function($http,$cookieStore,$rootScope){ 
 
    
 
    this.Login = function (username, password, callback) { 
 

 
      $http({ 
 
     method: "POST", 
 
     //headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
 
     url: 'users/authenticate', 
 
     data: {email: username,password:password} 
 
    }) 
 
    .success(callback); 
 
     }; 
 
     
 
});

永遠記住服務就像函數原型
我們不應該在服務的CAS返回對象。 Angular實例化對象並自動返回

+0

謝謝@ricky :) –