DEV Community

Discussion on: Provide simple value for components in Angular

Collapse
 
alleamitrano profile image
~AlleAmiDev~

Hi Daniel! I don't understand. Why would you want to inject services this way when they can be provided in Root and initialised in the constructor?

Collapse
 
danielpdev profile image
danielpdev

@Hi AlleAmiDev sorry, but maybe I wasn't enough clear about it.

You are right, using @Inject() it's not a desired way to inject services into your components, but that was just an example.

You should use @Inject('value') when you need to inject just a specific value and not a whole instance.

For ex:

providers: [
    {
      provide: "language",
      useValue: "en"
    }
  ],

and then in your components:

...
    @Inject("language") private language,
....

Did I answer your question? :-)