title: Inject Specific Service Instances into Multiple Instances of a Component in One Parent Component
body: <p>I'm working on a very large project right now, and we're trying to make our code as modular as possible. We have multiple Angular applications, and we've created a separate library of common UI components and an API kit with common services used across these applications. </p>
  
  <p>We're running into problems though when trying to build common components that need to work with services. For example, I'm working on an autocomplete component right now. That component should be able to fetch data from a remote source and filter the results based on what's been typed into the component's input field.</p>
  
  <p>That implementation is easy enough for one instance. I inject the autocomplete service in my autocomplete component's constructor, then I provide it on the parent. That gives me the flexibility to change to the implementation details of the service when I used it while still being able to create a reusable component that works with a defined interface.</p>
  
  <p>For example:</p>
  
  <p>The service where we want to define the interface used by our Autocomplete Component:</p>
  
  <pre><code>@Injectable()
  export class AutocompleteService {
    public url: string = 'my-api-default';
  
    constructor(http: Http) {}
  
    fetch(): any {
      return this.http.get(this.url);
    }
  }
  </code></pre>
  
  <p>The autocomplete component implementation:</p>
  
  <pre><code>@Component({
    selector: 'my-autocomplete',
    templateUrl: 'my-autocomplete.component.html'
  })
  export class MyAutocompleteComponent {
    constructor(private autocompleteService: AutocompleteService) {}
  
    getData() {
      return this.autocompleteService.fetch();
    }
    ...autocomplete logic...
  }
  </code></pre>
  
  <p>Now I can define a bear service that implements the Autocomplete service. I can hook up the bear service to my autocomplete component, so I can choose bear species in my form.</p>
  
  <pre><code>@Injectable()
  export class BearService {
    public url: string = 'bear-url';
  
    constructor(http: Http){}
  
    fetch() {
      return this.http.get(this.url);
    }
  }
  </code></pre>
  
  <p>Next, I define the parent that consumes my autocomplete component and provide the bear service to get my bear species data.</p>
  
  <pre><code>@Component({
    selector: 'my-form-component',
    template: `
      &lt;form&gt;
        &lt;my-autocomplete [(ngModel)]="bear"&gt;&lt;/my-autocomplete&gt;
        &lt;button type="submit"&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
    `,
    providers: [
      {provide: AutocompleteService, useClass: BearService}
    ]
  })
  export class MyFormComponent {
    ...component logic...
  }
  </code></pre>
  
  <p>So far, so good. </p>
  
  <p>My question arises when I need to build a large form that uses multiple autocomplete components. My boss tells me I need three autocomplete fields on this form, one for bear species, one for beet species, and one for a Battlestar Gallactica character. My first thought is to do this:</p>
  
  <p>I define the service instances:</p>
  
  <pre><code>@Injectable()
  export class BeetsService {
    public url: string = 'beets-url';
  
    constructor(http: Http){}
  
    fetch() {
      return this.http.get(this.url);
    }
  }
  
  @Injectable()
  export class BattleStarGallacticaService {
    public url: string = 'battlestar-gallactica';
    constructor(http: Http){}
  
    fetch() {
      return this.http.get(this.url);
    }
  }
  </code></pre>
  
  <p>Then I update the parent template and providers:</p>
  
  <pre><code>@Component({
    selector: 'my-form-component',
    template: `
      &lt;form&gt;
        &lt;my-autocomplete [(ngModel)]="bear"&gt;&lt;/my-autocomplete&gt;
        &lt;my-autocomplete [(ngModel)]="beet"&gt;&lt;/my-autocomplete&gt;
        &lt;my-autocomplete [(ngModel)]="battleStarGallactica"&gt;&lt;/my-autocomplete&gt;
        &lt;button type="submit"&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
    `,
    providers: [
      {provide: AutocompleteService, useClass: BearService},
      {provide: AutocompleteService, useClass: BeetService},
      {provide: AutocompleteService, useClass: BattlestarGalacticaService},
    ]
  })
  export class MyFormComponent {
    ...component logic...
  }
  </code></pre>
  
  <p>Now how can I tell which autocomplete component to use which service?</p>
  
  <p>I know what I have now will <em>always</em> use the last provider given for AutocompleteService, because that's how the Angular DI framework works.</p>
  
  <p>I also know I can't use multi-providers on this since Angular only defines them for <code>NG_VALIDATORS</code> and <code>NG_ASYNC_VALIDATORS</code>. </p>
  
  <p>So, does anyone have any idea how I can solve my problem? I don't care <em>how</em> the problem gets solve per se, but I still need to be able to:</p>
  
  <ol>
  <li>Define a service interface</li>
  <li>User that service interface in the reusable component</li>
  <li>Create a new service instance for my own needs that implements the original interface</li>
  <li>Be able to use multiple components that implement the same service interface using different service implementations within a single parent component</li>
  </ol>