How can I insert a string into a factory service?

I'm trying to inject a string into a factory service, but I'm confused about the error TS1206: Decorators are not valid here

.

I am using the following code -

export let serviceFactory = (http: Http, @Inject('TEMPLATE') template: string) => {
    return new Service(http, template);
}

      

I use my factory like this:

export let serviceProvider = {
    provide: Service,
    useFactory: serviceFactory,
    deps: [Http]
};

      

How can I insert a string into a factory service?

+3


source to share


1 answer


app.module.ts

export let serviceFactory = (http: Http, template: string) => {
  return new Service(http, template);
}

export let serviceProvider = {
  provide: Service,
  useFactory: serviceFactory,
  deps: [Http, [new Inject('TEMPLATE')]]
};

@NgModule({
  imports: [
    BrowserModule,
    HttpModule 
  ],
  declarations: [ AppComponent ],
  providers: [
    { provide: 'TEMPLATE', useValue: 'test' },
    serviceProvider,
  ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

      

service.ts



@Injectable()
export class Service {
  constructor(private http: Http, @Inject('TEMPLATE') template: string) {
      console.log(template);
  }
}

      

Plunger example

+3


source







All Articles