Search code examples
angularangular-pipe

How can I call a pipe with in a service in angular4?


I want to call a pipe with in a service.

just like

 export class MyService{

  constructor(private http: Http){}

  getValues(){
    this.http.get(baseUrl).pipe(//pipename) //I want to menton my custom pipe
  }
}

After completion of pipe I want to return that observables to component. Is it possible ?


Solution

  • Yes, you can call pipe in service file like this-

    import { DatePipe } from '@angular/common';
    class MyService {
    
      constructor(private datePipe: DatePipe) {}
    
      transformDate(date) {
        this.datePipe.transform(myDate, 'yyyy-MM-dd');
      }
    }
    

    as you have not provided any example of your use case , I am assuming DatePipe here in my example.

    Update

    export class MyService{
    
      constructor(private http: Http, private yourPipe: YourPipe){}
    
      getValues(){
        this.http.get(baseUrl).map(res => {
           return this.yourPipe.transform(res, ----whatever---);
        });
      }
    }