In Angular, a service is a class that is responsible for carrying out specific tasks, such as fetching data from a server, validating user input, or logging messages. Services provide a way to share data and functionality across different components in your application, promoting a clean separation of concerns and enhancing reusability.
Let’s build a simple service that fetches data from a hypothetical API. First, create a new service using Angular CLI:
ng generate service data
This will create two files: data.service.ts
and data.service.spec.ts
.
Open data.service.ts
and implement the DataService
as follows:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root', }) export class DataService { private apiUrl = 'https://api.example.com/data'; constructor(private http: HttpClient) {} fetchData(): Observable<any> { return this.http.get(this.apiUrl); } }
In this example:
@Injectable()
decorator to mark the class as a service that can be injected into other classes.providedIn: 'root'
syntax makes sure this service is available at the root level of the application.HttpClient
is injected into the service constructor to facilitate HTTP requests.To use our newly created service, we need to inject it into a component. Let’s modify a component to display data from our service.
app.component.ts
.DataService
within the constructor and use it to fetch data:import { Component, OnInit } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent implements OnInit { data: any; constructor(private dataService: DataService) {} ngOnInit(): void { this.dataService.fetchData().subscribe((response) => { this.data = response; console.log(this.data); }); } }
OnInit
lifecycle hook to call the fetchData()
method when the component initializes.Dependency Injection (DI) is a design pattern used in Angular that allows a class to receive its dependencies from external sources rather than creating them internally. This technique provides a way to manage dependencies efficiently.
Angular’s DI system allows you to:
In our earlier example, DataService
was injected into AppComponent
, demonstrating DI in action.
In Angular, you can control the scope of your services:
providedIn: 'root'
are available globally in your application.Let’s create a module-scoped service:
data.service.ts
to provide the service specifically in a module:import { Injectable } from '@angular/core'; @Injectable() export class DataService { // implementation unchanged }
app.module.ts
, register the service:import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { DataService } from './data.service'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule], providers: [DataService], // Registering service here bootstrap: [AppComponent], }) export class AppModule {}
In this case, if DataService
is injected into multiple components, they will share the same instance of the service.
In this post, we explored Angular Services and Dependency Injection, crucial concepts that enhance the architecture of Angular applications. With the understanding of how to create and utilize services in conjunction with Angular's DI framework, you’ll be well on your way to building efficient and modular applications. Happy coding!
24/10/2024 | Angular
24/10/2024 | Angular
24/10/2024 | Angular
24/10/2024 | Angular
24/10/2024 | Angular
24/10/2024 | Angular
24/10/2024 | Angular
24/10/2024 | Angular