Providers Array in Angular Tutorial šŸŽÆ

beginner
7 min

Providers Array in Angular Tutorial šŸŽÆ

Welcome to the Providers Array lesson! In this tutorial, we'll dive deep into understanding Providers Array in Angular - a powerful tool for managing services in your applications. By the end of this lesson, you'll be able to create, manage, and use services efficiently.

What are Providers in Angular? šŸ“

Providers are responsible for creating and managing services in Angular applications. They define the dependencies and their behavior for a service. The Angular Dependency Injection (DI) system uses providers to inject services into the components, directives, and other services that require them.

What is the Providers Array? šŸ’”

The Providers Array is a mechanism used to configure and group multiple providers for a single service. It allows you to define various provider instances for a service, and Angular will use the most appropriate one based on the context.

Creating a Service with the Providers Array āœ…

Let's create a simple service named DataService and add it to the Providers Array.

typescript
import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', // We'll discuss this later useClass: DataService, // Use our custom DataService class multi: true // This makes the service instanceable multiple times }) export class DataService { constructor() { } getData() { return 'Hello from DataService!'; } }

šŸ“ Note: The @Injectable decorator marks the class as a service, and providedIn indicates where the service should be available.

Injecting the Service āœ…

Now that we have our service, let's use it in a component.

typescript
import { Component, OnInit } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-root', template: ` <div> {{ data }} </div> ` }) export class AppComponent implements OnInit { data: string; constructor(private dataService: DataService) { } ngOnInit() { this.data = this.dataService.getData(); } }

šŸ“ Note: We inject the service using Angular's constructor-based DI and access its methods through the variable dataService.

Using the Providers Array for Different Service Instances šŸ’”

Let's create two different instances of the DataService with different configurations:

typescript
@NgModule({ declarations: [AppComponent], imports: [ BrowserModule, HttpClientModule ], providers: [ // Provide the DataService with the default configuration DataService, // Provide a different instance of DataService for our component { provide: DataService, useClass: CustomDataService } ], bootstrap: [AppComponent] }) export class AppModule { }

šŸ“ Note: We've added a new provider { provide: DataService, useClass: CustomDataService } that creates a custom instance of the DataService.

Accessing the Custom Service Instance āœ…

Now, we can use the custom instance of DataService in our component:

typescript
import { Component, OnInit } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-root', template: ` <div> {{ customData }} </div> ` }) export class AppComponent implements OnInit { customData: string; constructor(private customDataService: DataService) { } ngOnInit() { this.customData = this.customDataService.getData(); } }

šŸ“ Note: We inject the custom instance of DataService using the constructor and access its methods through the variable customDataService.

Understanding providedIn šŸ’”

The providedIn property in the @Injectable decorator is used to specify the module where the service should be available. Here are the options:

  • root: The service will be available throughout the entire application.
  • module: The service will be available only within the module where it's defined.
  • A specific module: The service will be available only within the specified module.

Wrapping Up šŸŽÆ

You've now learned about Providers Array in Angular and how to create, manage, and use services in your applications. In the next lesson, we'll dive deeper into services and dependency injection in Angular.

Quick Quiz
Question 1 of 1

What does the `@Injectable` decorator do in Angular?