2017-01-02 18 views
-1

私は角度サービスからコールバックを取得しようとしていますが、コールバック関数は機能しません。ここでhttpポストコールバックが作動していない角度ファクトリ

マイコントローラのコードである: - :

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; 
}); 
どちらも私は、コンソールで応答を取得 -

$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; 
    }) 
} 

私のサービスコードではありません。スコープは変更されません。 私はこの質問に言及しましたが、答えは私のために働いていません。あなたがサービスを作成している場合 $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にオブジェクトを返すべきではありません。角度オブジェクトをインスタンス化して自動的に返す

+0

ありがとう@ricky :) –

関連する問題