i am new to angular 5 . How to code a common function to show spinner for every HTTP request in angular 5.Please help me to implement this.
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
You can use Angular HttpInterceptor to show a spinner for all your requests, Here's a good medium article on how to implement an http interceptor
Also, you will have to create a spinner service/module and inject it in your http interceptor. Finally, in your intercept method you can use the
finally
rxJs method to stop your spinner. Here's a simple implementation:Enjoy !
Bonus: Here's a spinner service implementation example
You can create a service and then publish events to it while in the root level of the application subscribed to it. Let me explain.
broadcast.service.ts (this is your service)
Then in you can publish and subscribe to events like this:
your service level:
In your app component level:
finally in app.component.html:
This is very generic loosely coupled way to implement any loader you want.
Source Link
Create a service
Create Loader Interceptor
Now create a loader component then add in the app component
In Angular's interceptor I have used "do" operator.
onXhrStart function shows a loader and onXhrFinish hides it.
Entire, working source code and demo are here
This has nothing to do with HttpClient or HTTP Requests. It is a question of how to handle asynchronous calls in general (HTTP or not).
You should have
and in the ts-file: