liveBook · Manning

Provider

As explained in Pro Angular 16

A class provider is Angular dependency-injection configuration that maps a token to a class implementation.

A provider is Angular dependency-injection configuration that tells Angular which object should satisfy a dependency. A class provider maps a token, commonly a class type, to a class implementation. section 18.2.2 section 23.7.2

Register classes in a module's providers array when dependent classes should receive them through constructor injection. Angular inspects constructor parameters and resolves matching services from the configured providers. section 5.3.4 section 18.2.2

Registering a replacement implementation

A shorthand such as providers: [ProductRepository, StaticDataSource] registers classes as injectable services. An explicit class provider selects a different implementation for a token: providers: [{ provide: ErrorHandler, useClass: MessageErrorHandler }]. Angular then supplies MessageErrorHandler wherever ErrorHandler is requested. This replaces the default error handler while allowing the custom class to inject MessageService and NgZone. section 5.3.4 section 23.7.2

Switching data-source implementations

A feature module can replace a static data source with a REST implementation by removing StaticDataSource and registering RestDataSource: providers: [Model, RestDataSource]. Importing HttpClientModule supplies the HTTP dependency required by the REST-based service. section 23.3.2

Listing 23.15. Configuring an error handler in the message.module.ts file in the src/app/messages folder
import { NgModule, ErrorHandler } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { MessageComponent } from "./message.component";
import { MessageService } from "./message.service";
import { MessageErrorHandler } from "./errorHandler";

@NgModule({
    imports: [BrowserModule],
    declarations: [MessageComponent],
    exports: [MessageComponent],
    providers: [MessageService, 
        { provide: ErrorHandler, useClass: MessageErrorHandler }]
})
export class MessageModule { }

Use the complete module configuration to see the token-to-class mapping for a custom error handler. section 23.7.2

How Pro Angular 16 covers it

How deep it goes
Explains provider arrays, constructor resolution, shorthand class registration, and explicit useClass mappings, but defers broader provider forms.
Examples
Uses feature-module services, REST data sources, shared discount services, and custom HTTP error handling as concrete provider configurations.
What it assumes
Assumes familiarity with Angular classes and modules while introducing dependency injection, services, providers, and constructor dependencies.
What you can do after
Configure Angular modules to register services, swap implementations with useClass, and resolve dependencies through constructors.
How it connects
Connects providers with dependency injection, services, constructors, Angular modules, ErrorHandler, HttpClientModule, NgZone, and change detection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.