Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 interface for service

I want to develop a search component. Here is the following use case:

  1. This component calls a service with search's terms parameters.
  2. The service call the api endpoint and returns the resulting objects as a collection.
  3. The component display the results in the template.

I want to write only one search component able to call different service depending on the case. Imagine I have two service:

  1. SearchInMaleEmployeeService
  2. SearchInFemaleEmployeeService

Both of these services implements a search function returning a list of employee. I would like to tell my component which service depending on the case. In C#, we can use interface to tell the component constructor which service to use.

How can I do that in Angular2?

Bonus question: How can I say to my component which template to use to render the search results depending of the type of object returned by the service?

like image 795
Ben Avatar asked Oct 16 '16 08:10

Ben


People also ask

Why can’t we use interfaces as providers in angular?

Interfacing Services I mentioned above that Angular does not allow us to provide interfaces as providers because interfaces aren’t compiled by TypeScript. Therefore, when modelling a service as an interface, we actually have to use an abstract class.

How to create a service in Angular JS?

The following key steps need to be carried out when creating a service. Step 1 − Create a separate class which has the injectable decorator. The injectable decorator allows the functionality of this class to be injected and used in any Angular JS module.

How to implement di in angular components/services?

Also, DI in our Angular components/services can be implemented using either constructor or injector. Let's try creating a basic service which we'll call as LoggingService. It would contain a method to log some predefined string and append whatever param is passed to it. Inside logging.service.ts:

How do I connect to back end services in angular?

Communicating with backend services using HTTP link Most front-end applications need to communicate with a server over the HTTP protocol, in order to download or upload data and access other back-end services. Angular provides a client HTTP API for Angular applications, the HttpClient service class in @angular/common/ http.


2 Answers

You can achieve this via dependency injection.

As you said, create two different services implementing same ISearchService interface.

When using SearchComponent, provide appropriate service class from module to ServiceComponent.

Your SearchComponent would look like

  constructor(private searchService: ISearchService) {}

And when using SearchComponent at different places provide service instance:

providers: [
  { provide: ISearchService, useValue: SearchInMaleEmployeeService}
]

or

providers: [
  { provide: ISearchService, useValue: SearchInFemaleEmployeeService}
]

More information about Angular2 dependency injection here.

Update:

As pointed out by Ben

Provide statement needs to be coded as

provide('ISearchService', {useClass: SearchInMaleEmployeeService})

And to inject the class to component:

constructor(@Inject('ISearchService') private searchService:ISearchService) {}
like image 56
Sefa Avatar answered Oct 19 '22 12:10

Sefa


Yes, you can do it like Sefa Ümit Oray answered above. But as I understand, you are trying to filter two type of object in list and you want to use both. So why you don't write a service that has two difference search methods. Or you can write a method that do search in both types of object.

As you ask, you can use instance of to check the type of object. Then, use Pipe combine with ngIf to do render what you want.

https://angular.io/docs/ts/latest/guide/pipes.html https://angular.io/docs/ts/latest/api/common/index/NgIf-directive.html

like image 23
Clite Tailor Avatar answered Oct 19 '22 11:10

Clite Tailor