Tuesday, 9 April 2019

Angular - scope of services

@angular/cli: 7.3.7

Services are a powerful tool in Angular when it comes to storing data or application state. This article is a quick refresher on the various scopes supported by an Angular service.

In order to allow a service to be used, it needs to be declared in the provider section of a module or component.

However, depending on the place where the service is registered, its scope varies accordingly. Let us see how:

a) Service registered in AppModule.ts: This is the root module of your application, and a service registered here holds its state throughout the application. Any value changed here is accessible from any component in the AppModule. This is primarily done for storing data on a global scale, e.g., login state or  client token, etc.

b) Service registered in your custom module: The state of the service holds so long the application navigates to any of the router links registered in this module's router.

c) Service registered in a component: As seems obvious, the scope of this service instance is till the specific component is in use. The state is lost when we navigate away from this component. We register a service on a component only when we need specific functionalities for a component, which does not depend on application state.

We will see a quick demo to illustrate this behaviour.

This sample application has two components (one and two). Two services (LocalService and GlobalService) are in use. The LocalService is defined at the OneComponent, and the GlobalService is defined at AppModule.

GlobalService in AppModule, LocalService in OneComponent

Each service has a variable, which we would update from the OneComponent.

Variables in both services updated

Now the variables in both the services are updated. No problem with that. Let's navigate to the "two" component and come back to "one".

LocalService reset, GlobalService retained
We see that the variable in LocalService has been reset to its original value, while the variable in GlobalService has retained its value.

This is expected since the GlobalService was defined in AppModule and it holds its state throughout the life-cycle of the application. The LocalService was defined in OneComponent, and its state was lost the moment we navigated to a different component.

You can download the source from GitHub.


Thursday, 4 April 2019

Angular - a pipe demo

@angular/cli: 7.3.7

I was working with an interesting UI requirement, and I ended up with developing a sweet little pipe. Before I go into that, I'd describe the use case.

I have a model which has a trip plan name, and a list of locations. I am supposed to display each trip plan as a bootstrap list item. The list item should have the plan name as a list header, and the locations as a list-item-text.

Now of course, keeping in mind the small form factor, you need to ensure that when the number of locations grow in size, they should be fitted nicely into the screen.

Imagine I have an array of locations such as ['New Delhi', 'Kolkata', 'Chennai', 'Mumbai', 'Bangalore', 'Hyderabad']. I simply cannot display all of them on the screen, and going on to the next line is certainly not a great user experience. But what if the user could specify a max length, and beyond that the strings would be shortened with a "and 3 more..." or "and 5 more..."?

As an example: ['New Delhi', 'Kolkata', 'Chennai', 'Mumbai', 'Bangalore', 'Hyderabad'] gets shortened to Mumbai, Kolkata and 4 more..

To implement this, I have used a pipe, which works against a string array. It uses two methods to shorten - bestfit and sequential.

* 'bestfit': This means that that array will be sorted in ascending order of length of array items and maximum number of items will be fitted in the output string.
* 'sequential': This means that the array will be taken as is, and the first n possible items will be fitted.

Take a look at the text-shorten.pipe.ts file and play around with the 2 methods.

Source Code: GitHub