Here's how I would do it, moreover, the OOP path.
Services:
export interface IGraphServices {
getGraphData(): string;
}
@Injectable()
export class EmployeeAttendanceService implements IGraphServices {
getGraphData(): string {
return "Employee Attendance Data";
}
}
@Injectable()
export class ProductSalesService implements IGraphServices {
getGraphData(): string {
return "Product Sales Data";
}
}
component:
@Component({
selector: 'graph',
template: '<div>Graph component</div>'
})
export class GraphComponent implements OnInit {
@Input('service') service: number;
constructor(@Inject('IGraphServices')private providerService: IGraphServices[]) {
}
ngOnInit(): void {
console.log(this.providerService[this.service].getGraphData());
}
}
In your NgModule providers:
providers: [
{
provide: 'IGraphServices', useClass: EmployeeAttendanceService, multi: true
},
{
provide: 'IGraphServices', useClass: ProductSalesService, multi: true
}
]
Using:
<graph [service]="0"></graph>
<graph [service]="1"></graph>
source
share