Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 How can I inject a custom provider into a service?

Using typescript, In main.ts I have:

let myProvider = provide("message", { useValue: 'Hello' });

bootstrap(AppComponent, [
  myProvider
]);

How can I inject this into my service (which is in a different file)? (Keep in mind I'm not using the @Component annotation.

like image 319
williamsandonz Avatar asked Mar 13 '23 11:03

williamsandonz


1 Answers

I would use the @Inject decorator:

@Injectable()
export class SomeService {
  constructor(@Inject('message') message:string) {
  }
}

Don't forget to configure the service provider. For example when bootstrapping your application:

bootstrap(AppComponent, [ SomeService, myProvider ]);
like image 181
Thierry Templier Avatar answered Mar 15 '23 12:03

Thierry Templier