Ich arbeite an einem Szenario, wo auf der Seite laden ich 2 $http.get()
Anfrage haben. Ein $http.get()
sind unabhängig von einem anderen $http.get()
. Alles funktioniert gut. Aber in einigen Situationen wird meine 2. $http.get()
Anfrage vor der 1. $http.get()
ausgeführt und ich bin nicht in der Lage, die gewünschte Ausgabe zu bekommen. Wie können wir die Anfrage ketten? Da ich neu bei AngularJS bin, habe ich nicht viel Ahnung.
$scope.onload = function()
{
var responsePromise1 = $http.get(1st rest call);
responsePromise1.success(function(data) {
console.log(data.platform.record);
$scope.records=data.platform.record;
});
responsePromise1.error(function(data, status, headers, config) {
alert("ajax failed");
});
var responsePromise2 = $http.get(2nd rest call);
responsePromise2.success(function(data2) {
console.log(data2.platform.record);
$scope.records2=data2.platform.record;
});
responsePromise2.error(function(data2, status, headers, config) {
alert("ajax failed");
});
}
$scope.butto = function(datafrom1st,datafrom2nd)
{
var responsePromise3 = $http.get(3rd rest call);
responsePromise3.success(function(data3) {
console.log(data3.platform.record);
$scope.records3=data3.platform.record;
});
responsePromise1.error(function(data3, status, headers, config) {
alert("ajax failed");
});
}
<body>
<div ng-init="onload()">
<div ng-repeat="record in records">
{{record.id}}
</div>
<div ng-repeat="record2 in records2">
{{record2.name}}
</div>
<button type="button" class="btn btn-primary btn-sm ng-cloak"
style="padding-bottom:25px;font-weight:bold;"
ng-init="butto(record.id,record2.name)">
{{record3.data}}
</button>
</div>
</body>
Ihre 'responsePromise1' wird vor' responsePromise2' ausgeführt, aber sie sind beide asynchron Call, was bedeutet, dass wir nicht garantieren können, wer zuerst fertig wird. Wenn Sie die Anfrage verketten müssen, muss Ihre 'responsePromise2' innerhalb des' sucess' von 'responsePromise1' liegen. – Hereblur
Wenn sie unabhängig sind, sollten Sie sie nicht verketten. –
Wenn sie voneinander unabhängig sind, verwenden Sie eine Promise All. Im 'then' Callback wird es beide Antworten haben. – Hoyen