Skip to content

Instantly share code, notes, and snippets.

@chgc
Created June 8, 2017 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chgc/84c0ea44307186420f5f3959a2470864 to your computer and use it in GitHub Desktop.
Save chgc/84c0ea44307186420f5f3959a2470864 to your computer and use it in GitHub Desktop.
use Map 來儲存 service
import {Component, Inject, Injector} from '@angular/core';
import {serviceToken} from './app.module';
import {IService} from './iservice';
import {Service1Service} from './service1.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
private services = new Map<string, IService>();
constructor(private injector: Injector) {
(<IService[]>injector.get(serviceToken)).forEach(s => {
this.services.set(s.kind, s);
});
}
callFunction(kind: string): IService|undefined {
return this.services.get(kind);
}
callService(kind: string) {
const s = this.callFunction(kind);
console.log(s.log());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment