Angular 2 dependency injection for decorators

Is there any way to inject a service dependency into a @Component decoration, something like this?

    selector: injectedService.getPrefix() + 'my-component'
export class MyComponent { }

Or, if not, might it be possible to subsclass @Component and inject a dependency into the subclass to achieve a similar result?

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

update >= RC.5 @NgModule({ ... }) export class AppModule { ngDoBootstrap(moduleRef) { appInjector(moduleRef.injector); } } appInjector implementation see below original <= RC.5 This is not directly supported by Angular2. You can store the injector outside of your Angular app and then reference it from there like demonstrated as a workaround for the @CanActivate() decorator in (Plunker example) In main.ts the injector is assigned to appInjector bootstrap(App, [ Auth, HTTP_PROVIDERS, ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy}) ]).then((appRef: ComponentRef) => { // store a reference to the application injector appInjector(appRef.injector); }); app-injector.ts let appInjectorRef: Injector; export const appInjector = (injector?: Injector):Injector => {… Read more »