2016-03-28 5 views
0

2つの$ http(.post)要求をどのように連結して、もう一方を呼び出すことができますか? これは機能していないような私の機能です。

$scope.signup = function() { 
    console.log("New user"); 
    $http.post('/signup',{ 
     email: this.email, 
     password: this.password 
    }).then(function success($http){ 
     return $http.post('api/userData/init'); //error 
    }).then(function redirect($state){ 
     return $state.go('profile'); 
    });    

} 
+0

エラーは何ですか?あなたは2番目のポストでコールバック関数が必要です。 '.then(function success($ http){...' –

答えて

0

最初のコールバックでは、$httpを上書きしています。 then関数は、http呼び出しの結果を引数として取る関数を想定しています。試してみてください:

$scope.signup = function() { 
    console.log("New user"); 
    $http.post('/signup',{ 
     email: this.email, 
     password: this.password 
    }).then(function(){ 
     return $http.post('api/userData/init'); 
    }).then(function(){ 
     return $state.go('profile'); 
    });    

} 
+0

あなたの説明のために多くのおかげです。 – Ugo

+0

素晴らしい。 – yarons

2

は、あなただけのノッチ

$scope.signup = function() { 
    console.log("New user"); 
    $http.post('/signup',{ 
     email: this.email, 
     password: this.password 
    }).then(function(data){ 
     $http.post('api/userData/init').then(function(newData){ 
      $state.go('profile'); 
     }); 
    }); 

} 
1

でそれを逃したそれとも、また、明示的に呼び出しで$ q個の約束を使用することができます。

//$q needs to be injected in the controller or service 
    var q = $q.deffer(); 

    $http.post('/signup',{ 
     email: this.email, 
     password: this.password 
    }).then(function(data){ 
     //on success 
     q.resolve(data); 
    }, function(){ 
     //on fail 
     q.reject('Failed'); 
    }); 

    //second call after the first is done 
    q.promise.then(function(firstCalldata){ 
     //if you need you can use firstCalldata here 
     $http.post('api/userData/init').then(function(newData){ 
      $state.go('profile'); 
     }); 
    }) 
関連する問題