Im working on a scenario where on page load i have 2 $http.get()
request. One $http.get()
are independent on another $http.get()
. Every thing works fine.But in some situation my 2nd $http.get()
request executes before the 1st $http.get()
and i'm not able to get the desired output. How can we chain the request? Since i'm new to AngularJS i don't have that much idea.
$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>
responsePromise1
execute beforeresponsePromise2
but they are both asynchronize call, which mean we can not guarantee who will finish first. If you need to chain the request ,yourresponsePromise2
must be inside thesucess
ofresponsePromise1
ng-init
and use services to call APIs. Post your html and routes for a better answer