我正在使用angularjs $ http.get()請求。在頁面的負載下,我正在執行兩個rest api call。api執行正確。我有一個按鈕它使用來自兩個其他API調用的數據。我面臨的一個問題是我的函數在$ http.get()響應之前被執行,並且我無法獲得所需的結果。如何使我的函數在來自兩個$ http.get()請求的響應之後執行。任何人都可以停留在這一點上help.i'm
var responsePromise5 = $http.get("1st rest call");
responsePromise5.success(function(data1) {
$scope.id = data1.platform.user.id;
\t var responsePromise = $http.get("2nd rest call");
responsePromise.success(function(data2)
{
console.log(data2.platform.record);
$scope.records= data2.platform.record; \t \t
});
responsePromise.error(function(data2, status, headers, config) {
alert("AJAX failed!");
}); \t
});
responsePromise5.error(function(data1, status, headers, config) {
alert("AJAX failed!");
}); \t
$scope.hello = function(a,b)
{
//here i want to call another rest api
}
<div ng-repeat="record in records">
{{record.name}}
<button ng-init=hello(record.unin,id)>abc</button>
</div>
呼叫後的$ scope.hello功能的響應後調用$ http.get – Hmahwish
的成功請參閱[AngularJS $ q服務API參考 - 鏈接承諾](https://docs.angularjs.org/api/ng/service/$q#chaining-promises)。另請參閱[SO:爲什麼不建議使用角度$ http成功/錯誤方法?從v1.6中刪除?](http://stackoverflow.com/a/35331339/5535245)。 – georgeawg