Angular 2 dependency injection for decorators

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

@Component({
    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?

1
Leave a Reply

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

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 https://github.com/angular/angular/issues/4112#issuecomment-153811572. (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 »