[Fixed] Inject additional parameters to Angular 10 service


I’m trying to inject additional parameters to my service. I’m following this tutorial https://www.techiediaries.com/pass-parameters-angular-10-service-inject/

But when I run my program I get NullInjectorError: No provider for param!

Should this even work because that look so simple I can’t understand what I possibly can do wrong there.

Why I need to do this is a have a service that will need to know object id to work properly. How to get this id depends on where this service is injected.

But to get things going I’d like to get this hard coded value work first.

PS. If I delete that private param: string from service constructor service gets injected correctly.


you need to provide your parameter from somewhere in the app.module.ts providers array:

for example, an apiUrl field got from environments file:

providers: [
      provide: 'apiUrl',
      useValue: environment.apiUrl

where apiUrl is declared in environment.ts file.

then, inject in your service by the constructor:

    private http: HttpClient,
    @Inject('apiUrl') private baseUrl: string
  ) {

updated: passing service parameter from component

"You can put the providers in the component, if the service itself
also is provided in the component. The values need to be provided at
the same level as the service or higher."

Leave a Reply

(*) Required, Your email will not be published