Search code examples
angularangular-services

Why don`t we need to add built-in Angular Services into provider but it still works?


I am following a tutorial and it uses "Title" built-in service as an example.

Importing service to component Ts file

import { Title } from '@angular/platform-browser';

Then injecting to constructor and setting title.

constructor(private title: Title){ this.title.setTitle('My Title'); }

If I run application, it works fine and I don`t get any "no provider for Title" error as it is not provided into any where at all. But this is violating Angular Service rules.

Can you please explain if built-in services are exception when it comes to adding to provider?


Solution

  • As the documentation describes the Title service is provided in the root of the application: https://angular.io/api/platform-browser/Title#provided-in

    This means that the Title service is defined with providedIn configuration. providedIn at the Injectable gives an alternative approach for providing dependencies through DI, if this is used, there is no need to provide the service through the providers array in the module config. See more details about providedIn here: https://angular.io/guide/providers#providing-a-service