我有2個服務
1)one.service.ts
2)two.service.ts
我有一個組件 - dashboard.component.ts
import { Component, ViewEncapsulation, Inject } from '@angular/core';
import { OneService } from '../services/one.service';
import { TwoService } from '../services/two.service';
@Component({
selector: 'dashboard',
encapsulation: ViewEncapsulation.Emulated,
styleUrls: ['./dashboard.less'],
templateUrl: './dashboard.html'
})
export class DashboardComponent {
constructor() {
// Here how do I get service instance based on some this condition.
if(true) {
/* Service **one.service.ts** to be injected */
} else {
/* Service **two.service.ts** to be injected */
}
}
}
使用'useFactory'或'injector.get' – yurzui
或者可能創建DashboardComponent的特定實例。看起來奇怪這個組件是多態的,以至於使用兩個服務。我可能錯了,當然,因爲我們錯過了域的上下文 – Sebas
檢查這個類似的問題,我要求正確處理angular2中的IoC:http://stackoverflow.com/a/42422835/1291428 – Sebas