Show loader on every request in Angular 2

Ivan Radunović
Beautiful Angular
Published in
5 min readJan 23, 2017

--

Providing feedback to our users is essential for good user experience. Because Angular is single page application when user navigates to new page browser won’t display usual loading animation.

Instead user will be presented with empty component and only after data is loaded (from backend) Angular will update views,.. unless you show loading animation from inside Angular.

Live Demo GIF

I know it sounds like simple functionality but I noticed that many developers are asking how to show loader on every http request, so lets start..

Custom Http Service

First thing needed is custom http service which extends built-in http class.

In one of my previous posts I wrote about custom http service for JWT authentication, same code is good for this post also. Check that tutorial here.

Full project is available on GitHub it includes code from all of my older posts, you can focus your attention on loader part only.

Code above is showing how I currently handle get requests from custom Http service.

I have full control over this request so I can call show loader method before call to get method of the super class. I can also call hide loader method from onEnd method, cause at that moment request to the backend will be completed.

In onEnd method I’ll call hideLoader method:

I still did not create these loader methods in the custom class I’ll construct them as simple logging methods, just temporarily until I create loader service.

Console

Custom service works well

Loader

Idea is to have loader component which will be included in the main app component and using loader service Angular will trigger visibility state of loader component.

Loader component

This component holds html and css code for our loader, it will also posses subscription for loader state from Loader Service. Based on the state of loader it will show or hide html code of the loader.

Loader component html

I am a bit lazy so I will use Angular Material Progress bar component as the loader.

Angular Material requires imports in app.module.ts and core.module.ts, I’ll also have to include the theme in index.html file.

In the app.module.ts imports array item looks like this:

And in core.module.ts imports array:

I am using Core Module as place where I put services and components required for every action, in this case custom Http Service, Loader Service and Loader Component.

In index.html I am adding pink theme for Angular Material:

Loader service

This service is pretty simple, it posses private property for the subject and public property, observable for that subject.

LoaderState is only an interface which holds one boolean property:

Loader css

I want to display loading progress bar right under address bar in full width, so it will be positioned in absolute manner.

Directory structure

All loader related files are stored in dedicated directory inside the core.

Importing loader

Because loader is part of the Core Module I will import it there:

And CoreModule is imported in imports array of app.module.ts.

Back to HttpService

I need to inject LoaderService through the constructor:

Show/Hide methods

I already called visibility methods from http request, now I’ll update them:

Http service factory

Services which extend build-in classes are required to have service provider defined and the factory. I am storing my factories into _factories directory. From CoreModule decorator you could see how am I defining service provider.

And this is the factory:

With this Http service is completed but we will see no loader cause it is not used in the App component.

Inside app.component.html I’ll insert angular-loader tag on the first line.

Closing

Now we have functional loader on every get request, if you are curious to see how it’ll look checkout Live Demo. Try clicking Load New button few times to see it in action.

Source code

Entire source code is available on GitHub, feel free to clone, fork and contribute. And please do not open new issues because of that pink loader color, my designer is on vacation ;)

--

--