Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading