I need to inject a service into another service in an Angular 2
application.
After reading the docs I deduced, that the best approach is to use a Factory Provider. However, two questions have arisen:
1) The docs recommend the creation of a HeroServiceProvider
class with two "code segments":
let heroServiceFactory = (logger: Logger, userService: UserService) => {
return new HeroService(logger, userService.user.isAuthorized);
};
export let heroServiceProvider =
{ provide: HeroService,
useFactory: heroServiceFactory,
deps: [Logger, UserService]
};
My question is how should the class generally look like? Where should one add the above code segments?
2) How should/could one use this factory? I see, it should be imported as:
import { heroServiceProvider } from './hero.service.provider';
@Component({
selector: 'my-selector',
template: `
`,
providers: [heroServiceProvider]
})
How could then the desired parametrized service retrieved and accessed?