Unveiling the Magic of Angular Services and Dependency Injection
Introduction: Angular, developed and maintained by Google, is a popular open-source web application framework used for building dynamic single-page applications (SPAs). In this in-depth tutorial, we will delve into two foundational features of Angular: Services and Dependency Injection. By mastering these concepts, you'll be empowered to create modular, maintainable, and efficient Angular applications.
Section 1: What are Angular Services?
1.1 Understanding Services In Angular, a service is essentially a class that provides reusable data or functionality across various components. Services can be thought of as the workhorses that handle specific tasks such as data retrieval, logging, or user authentication, irrespective of the view.
1.2 Creating a Service You can create an Angular service using the Angular CLI. For example, let's create a service that retrieves a list of users:
ng generate service user
This command creates a user.service.ts
file, which will contain the service logic.
1.3 Service as a Singleton One of the benefits of using services is that they are singleton; there is only one instance of a service per injector. This means that you can use a service to share data and functionality throughout your application.
Section 2: Dependency Injection (DI)
2.1 Introduction to DI Dependency Injection (DI) is a coding pattern in which a class receives its dependencies from an external source rather than creating them internally. In Angular, DI is a first-class citizen and is used extensively.
2.2 Why Use Dependency Injection?
Decoupling: DI decreases the coupling between a class and its dependencies.
Easier Testing: You can provide mock dependencies to a class, which makes unit testing easier.
Improved Maintainability: Managing dependencies centrally is more maintainable.
2.3 Injecting a Service Injecting a service into a component is straightforward. You need to include it in the component's constructor, and Angular takes care of the rest. Example:
import { UserService } from './user.service';
constructor(private userService: UserService) { }
Section 3: Building an Example Application
3.1 Setting up the Angular Application First, let's create a new Angular application using Angular CLI:
ng new angular-services-example
cd angular-services-example
3.2 Creating the User Service Let's create a UserService
that provides a list of users. Modify the user.service.ts
file as follows:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class UserService {
users = [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com' },
];
constructor() {}
getUsers() {
return this.users;
}
}
3.3 Creating a User Component Now, let's create a component to display the list of users:
ng generate component user-list
Inject the UserService
into the UserListComponent
and use it to retrieve the user list:
import { Component, OnInit } from '@angular/core';
import { UserService } from '../user.service';
@Component({
selector: 'app-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.css'],
})
export class UserListComponent implements OnInit {
users: any[];
constructor(private userService: UserService) {}
ngOnInit() {
this.users = this.userService.getUsers();
}
}
3.4 Displaying the Users Edit user-list.component.html
to display the list of users with their names and emails:
<ul>
<li *ngFor="let user of users">
{{ user.name }} - {{ user.email }}
</li>
</ul>
3.5 Root Component Modification Modify the app's root component to include the user list. Edit app.component.html
to the following:
<app-user-list></app-user-list>
3.6 Running the Application Finally, let's run the application using the Angular CLI:
ng serve
This will launch the development server. You can view your application by navigating to http://localhost:4200
your browser.
Section 4: Dependency Injection in Depth
4.1 Understanding Providers A provider is an instruction to the DI system on how to obtain a value for a dependency. Providers can be part of modules or components. They determine how services are created.
4.2 @Injectable Decorator The @Injectable
decorator is used to denote that a class can be used with the dependency injector. It can be used to configure a provider that will create a new instance for each injection.
Example:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class UserService {...}
4.3 Hierarchical Dependency Injection Angular's DI system is hierarchical. When Angular needs to create an instance of a component, it first looks for a provider in the component’s injector. If it doesn’t find a provider, it goes up to the parent component until it reaches the root module.
Section 5: Advanced Usage of Services
5.1 Sharing Data Between Components Services can also be used to share data between components. By storing data in a service, you ensure that you're working with a single source of truth throughout your application.
Example shared-data.service.ts
:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class SharedDataService {
private dataSource = new BehaviorSubject<any>(null);
currentData = this.dataSource.asObservable();
constructor() {}
changeData(data: any) {
this.dataSource.next(data);
}
}
5.2 Using Services for Communication Services are not just for sharing data; they can also be used for communication between components. For example, using observables in a service, components can subscribe to an observable to receive notifications of events.
Example:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class NotificationService {
private notificationSubject = new Subject<any>();
notifications$ = this.notificationSubject.asObservable();
notify(notification: any) {
this.notificationSubject.next(notification);
}
}
Section 6: Common Pitfalls and Best Practices
6.1 Singleton Services Be cautious with the singleton nature of services. Mutable shared state in a singleton service can cause unexpected behavior if not managed properly.
6.2 Lazy Loading For larger applications with many services, it's advisable to use lazy loading. This means that Angular will only load the services that are needed for a particular part of your application.
6.3 Testing Services Always write tests for your services. Since services often contain important business logic, it’s crucial they work correctly. Use Angular’s testing tools to write unit tests for services.
Conclusion: Angular's Services and Dependency Injection are powerful tools in your development toolkit. They help in creating scalable and maintainable code. By mastering these concepts, you can write more modular code that is easier to understand, test, and debug. Keep an eye on the performance and structure of your services, and don’t be afraid to refactor as your application grows.
Subscribe to my newsletter
Read articles from Ivan Soria directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ivan Soria
Ivan Soria
Experienced Computer System Engineer with over 10 years of industry expertise in various roles such as developer, code reviewer, architect, and team lead. Successfully delivered more than 20 projects while leveraging a diverse skill set and a deep understanding of software development methodologies. Proficient in technologies including .NET, C#, Xamarin Forms, Xamarin Native, MAUI, HTML5, CSS3, JavaScript, TypeScript, Angular, React, Azure, SCRUM, and SQL.