How to use factory provider?
<p>I need to inject a service into another service in an <code>Angular 2</code> application.</p>

<p>After reading the docs I deduced, that the best approach is to use a <a href="https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#injector-providers" rel="noreferrer">Factory Provider</a>. However, two questions have arisen: </p>

<p>1) The docs recommend the creation of a <code>HeroServiceProvider</code> class with two "code segments":</p>

<pre><code>let heroServiceFactory = (logger: Logger, userService: UserService) =&gt; {
  return new HeroService(logger, userService.user.isAuthorized);
};

export let heroServiceProvider =
  { provide: HeroService,
    useFactory: heroServiceFactory,
    deps: [Logger, UserService]
  };
</code></pre>

<p>My question is how should the class generally look like? Where should one add the above code segments?</p>

<p>2) How should/could one use this factory? I see, it should be imported as:</p>

<pre><code>import { heroServiceProvider } from './hero.service.provider';

@Component({
  selector: 'my-selector',
  template: `
  `,
  providers: [heroServiceProvider]
})
</code></pre>

<p>How could then the desired parametrized service retrieved and accessed?</p>