liveBook · Manning

Providers property

As explained in Pro Angular 16

The providers property of Angular module metadata lists services and other objects made available through Angular dependency injection.

The `providers` property tells Angular which services are available to resolve dependencies. In an `@NgModule` definition, it registers shared objects without requiring a parent component to create them and pass them through template bindings. section 19.2.3 section 18.2.2

Register a shared service

Define `DiscountService` with `@Injectable()`, add it to a module’s `providers` array, and request it in a component constructor: `constructor(public discounter: DiscountService) {}`. Angular examines the constructor argument when creating the component and supplies the registered service. Discount editor and display components can then use the service without input properties or a shared object owned by their parent. section 18.2.2 section 18.5

Listing 19.17. The contents of the common.module.ts file in the src/app/common folder
import { NgModule } from "@angular/core";
import { PaAddTaxPipe } from "./addTax.pipe";
import { PaAttrDirective } from "./attr.directive";
import { PaCategoryFilterPipe } from "./categoryFilter.pipe";
import { PaCellColor } from "./cellColor.directive";
import { PaCellColorSwitcher } from "./cellColorSwitcher.directive";
import { PaDiscountPipe } from "./discount.pipe";
import { PaDiscountAmountDirective } from "./discountAmount.directive";
import { PaIteratorDirective } from "./iterator.directive";
import { PaStructureDirective } from "./structure.directive";
import { PaModel } from "./twoway.directive";
import { DiscountService } from "./discount.service";
import { ModelModule } from "../model/model.module";
 
@NgModule({
    imports: [ModelModule],
    providers: [DiscountService],
    declarations: [PaAddTaxPipe, PaAttrDirective, PaCategoryFilterPipe,
        PaCellColor, PaCellColorSwitcher, PaDiscountPipe,
        PaDiscountAmountDirective, PaIteratorDirective, 
        PaStructureDirective, PaModel],
    exports: [PaAddTaxPipe, PaAttrDirective, PaCategoryFilterPipe,
        PaCellColor, PaCellColorSwitcher, PaDiscountPipe,
        PaDiscountAmountDirective, PaIteratorDirective, 
        PaStructureDirective, PaModel]
})
export class CommonModule { }

The utility module places `DiscountService` in `providers` while listing its directives and pipes separately in `declarations`. When the feature module is loaded, its service providers are added to the root module’s providers and are available throughout the application. section 19.3.2

Do not confuse `providers` with `declarations` or `exports`: those properties prepare directives, components, and pipes for templates and control which declarables an importing module can use. A utility module may import another module for services its own features depend on. section 19.3.2

Register a validation directive

A directive can also have a `providers` property. The custom price validator registers itself with `NG_VALIDATORS` using `useExisting` and `multi: true`; without that registration, Angular will not apply it during validation. This is a directive-level provider, rather than an entry in the module’s service-provider list. section 22.3.1

...
providers: [{provide: NG_VALIDATORS, 
        useExisting: HiLowValidatorDirective, multi: true}]
...

The focused configuration shows the validator’s provider token and the `useExisting` and `multi` options. section 22.3.1

How Pro Angular 16 covers it

How deep it goes
Works through service registration and constructor injection, then shows feature-module and validation-directive provider configurations without detailing injector internals.
Examples
Uses discount editor and display components, a utility feature module, and an `NG_VALIDATORS` registration for a price validator.
What it assumes
Assumes familiarity with TypeScript classes and components while explaining service decorators and constructor-based dependency injection.
What you can do after
Operate/configure it to register shared services in a module or a custom validator in a directive.
How it connects
Connects dependency injection and constructor parameters with `@Injectable`, `@NgModule`, feature modules, and custom form validation.

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.