我有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