How to Provide Service at Component Level in Angular
providers array in its decorator. This creates a new instance of the service unique to that component and its children, isolating it from other parts of the app.Syntax
In Angular, you provide a service at the component level by adding it to the providers array inside the @Component decorator. This tells Angular to create a new instance of the service for that component only.
Here is the syntax:
@Component: Decorator that defines the component metadata.providers: Array where you list services to provide at this component level.MyService: The service class you want to provide.
import { Component } from '@angular/core'; import { MyService } from './my-service.service'; @Component({ selector: 'app-example', templateUrl: './example.component.html', providers: [MyService] }) export class ExampleComponent { constructor(private myService: MyService) {} }
Example
This example shows a component that provides its own instance of a service. The service has a counter that increments each time a method is called. Because the service is provided at the component level, each component instance has its own counter.
import { Component, Injectable } from '@angular/core'; @Injectable() export class CounterService { count = 0; increment() { this.count++; return this.count; } } @Component({ selector: 'app-counter', template: ` <p>Count: {{ count }}</p> <button (click)="increment()">Increment</button> `, providers: [CounterService] }) export class CounterComponent { count = 0; constructor(private counterService: CounterService) {} increment() { this.count = this.counterService.increment(); } }
Common Pitfalls
One common mistake is providing the service both at the root and at the component level unintentionally, which can cause confusion about which instance is used.
Another pitfall is expecting the service state to be shared across components when provided at the component level; each component gets its own instance.
/* Wrong: Providing service at root and component level unintentionally */ import { Injectable, Component } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class DataService { data = 'shared'; } @Component({ selector: 'app-sample', template: '<p>{{dataService.data}}</p>', providers: [DataService] // This creates a new instance, hiding the root one }) export class SampleComponent { constructor(public dataService: DataService) {} } /* Right: Provide only at component level or root, not both */ @Injectable() export class DataService { data = 'component-level'; } @Component({ selector: 'app-sample', template: '<p>{{dataService.data}}</p>', providers: [DataService] // Only here }) export class SampleComponent { constructor(public dataService: DataService) {} }
Quick Reference
- Use
providersarray in@Componentto provide service at component level. - Each component instance gets a new service instance.
- Service state is isolated to that component and its children.
- Do not mix
providedIn: 'root'and component-level providers for the same service.