You can achieve this through dependency injection.
As you said, create two different services that implement the same ISearchService interface.
When using SearchComponent provide the appropriate service class from module to ServiceComponent .
Your SearchComponent will look like
constructor(private searchService: ISearchService) {}
And when using SearchComponent in different places, specify the service instance:
providers: [ { provide: ISearchService, useValue: SearchInMaleEmployeeService} ]
or
providers: [ { provide: ISearchService, useValue: SearchInFemaleEmployeeService} ]
More information on Angular2 dependency injection here .
Update:
As Ben noted
Provise statement must be encoded as
provide('ISearchService', {useClass: SearchInMaleEmployeeService})
And to add a class to the component:
constructor(@Inject('ISearchService') private searchService:ISearchService) {}
source share