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
