admin管理员组文章数量:1287268
I have 5 api calls in a page. some apis take 20 sec to give response. some take 30 sec to give response. some take 10 sec so, when first api gives its response, first api sets loading indicator false. then loading indicator disppears. But other apis are still working I want to show loading indicator until the five api calls response. can you please give me some idea to do the task.
code:
ponent.ts
loading = true;
ngInit() {
this.api1();
this.api2();
this.api3();
this.api4();
this.api5();
}
api1(){
this.loading=true;
this.apiService.api1.subscribe(response => {
loading = false;
}, error=>{
});
}
api2(){
this.loading=true;
this.apiService.api2.subscribe(response => {
loading = false;
}, error=>{
});
}
api3(){
this.loading=true;
this.apiService.api3.subscribe(response => {
loading = false;
}, error=>{
});
}
api4() {
this.loading=true;
this.apiService.api4.subscribe(response => {
loading = false;
}, error=>{
});
}
api5() {
this.loading=true;
this.apiService.api5.subscribe(response => {
loading = false;
}, error=>{
});
}
ApiService.service.ts:
api1(): any {
return this.http.get('apiurl');
}
api2(): any {
return this.http.get('apiurl');
}
api3(): any {
return this.http.get('apiurl');
}
api4(): any {
return this.http.get('apiurl');
}
api5(): any {
return this.http.get('apiurl');
}
I have 5 api calls in a page. some apis take 20 sec to give response. some take 30 sec to give response. some take 10 sec so, when first api gives its response, first api sets loading indicator false. then loading indicator disppears. But other apis are still working I want to show loading indicator until the five api calls response. can you please give me some idea to do the task.
code:
ponent.ts
loading = true;
ngInit() {
this.api1();
this.api2();
this.api3();
this.api4();
this.api5();
}
api1(){
this.loading=true;
this.apiService.api1.subscribe(response => {
loading = false;
}, error=>{
});
}
api2(){
this.loading=true;
this.apiService.api2.subscribe(response => {
loading = false;
}, error=>{
});
}
api3(){
this.loading=true;
this.apiService.api3.subscribe(response => {
loading = false;
}, error=>{
});
}
api4() {
this.loading=true;
this.apiService.api4.subscribe(response => {
loading = false;
}, error=>{
});
}
api5() {
this.loading=true;
this.apiService.api5.subscribe(response => {
loading = false;
}, error=>{
});
}
ApiService.service.ts:
api1(): any {
return this.http.get('apiurl');
}
api2(): any {
return this.http.get('apiurl');
}
api3(): any {
return this.http.get('apiurl');
}
api4(): any {
return this.http.get('apiurl');
}
api5(): any {
return this.http.get('apiurl');
}
Share
Improve this question
asked Apr 27, 2019 at 7:38
Kumaresan PerumalKumaresan Perumal
1,9562 gold badges30 silver badges39 bronze badges
1
- You might want to check out the operator decision tree-app in the docs: rxjs-dev.firebaseapp./operator-decision-tree – PatrikAkerstrand Commented Apr 27, 2019 at 8:09
4 Answers
Reset to default 9You can use rxjs forkjoin for the same. Forkjoin waits for all the request is plete and return the response when all the api call plete. Here is the example.
**ponent.ts**
isLoading: boolean;
constructor(private apiCallService: ApiSErvice) {}
ngOnInit() {
this.isLoading = true;
this.apiCallService.multipleApiCallFunction().subscribe(response => {
this.isLoading = false;
})
}
**ApiService.service.ts**
import { Observable, forkJoin } from 'rxjs';
multipleApiCallFunction() : Observable<any[]>{
const api1 = this.http.get('apiurl-1');
const api2 = this.http.get('apiurl-2');
const api3 = this.http.get('apiurl-3');
const api4 = this.http.get('apiurl-4');
return forkJoin([api1, api2, api3, api4]);
}
It seems this question is not active now. However I add the solution here to show spinner if anyone needs in future.
You can use this angular package to show spinner( loader ) whenever you call the api. It's very much helpful to show the spinner on initial load. You can also use regex's, methods and headers to filter the api's.
https://www.npmjs./package/ng-http-loader
Another way to achieve this if you don't want to use forkJoin or want to control each one individually, you can create a Subscription variable for each API call, and then in your HTML use an *ngIf="sub1 || sub2 || ... || sub5" to display and hide the loading indicator, it will be something like this:
//declare subscriptions like so
sub1: Subscription;
ngInit() {
sub1 = this.api1();
sub2 = this.api2();
sub3 = this.api3();
sub4 = this.api4();
sub5 = this.api5();
}
in your HTML loading bar tag add:
*ngIf="sub1 || sub2 || sub3 || sub4 || sub5"
You can simply use a variable(s) to store API results then load the content based on it's existance:
<div *ngIf = "apiResults; else elseBlock">
...
</div>
<ng-template #elseBlock>Loading...</ng-template>
版权声明:本文标题:javascript - how to show loading indicator on page loading in angular 7 until all apis response? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282376a2370080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论