Using Pipe in Ionic 2 / 3.0.1 doesn't work

Hi all,

I have tried to sort my list of vehicles based on their type of service; in order to do that i have used a custom pipe:

import { Injectable, Pipe } from 'angular2/core';

@Pipe({
    name: 'myfilter'
})
@Injectable()
export class MyFilterPipe {
    transform(vehicles: any[], args: any[]): any {
        return vehicles.filter(vehicle => vehicle.service_type == 'Replacement');
    }
}

And used it inside my html code:

Also. i have declared it inside app.module.ts under declarations array and imported it accordingly:

image

But it gives me, the following error:

image

Of course, if I remove the pipe, the error disappears.
Any ideas, please ?

Thank in advance

Donā€™t sort with a Pipe. That functionality was removed from Angular for a reason, and the Angular Pipe docs explain why. Sort in your controller, render in your HTML.

You are getting that error because the import statement you are using is for pretty older version of Angular.

Your import statement should be as follows to work:

import { Injectable, Pipe } from ā€˜@angular/coreā€™;

1 Like

Hi Aaron, how can we make use of pipe at controller level , all are showing only at html side, please help

Thanks,
Hash57

Hi, @gsimon

import { Injectable, Pipe } from '@angular2/core';

@Pipe({
    name: 'myfilter'
})
@Injectable()
export class MyFilterPipe {
    transform(vehicles: any[], args: any[]): any {
        return vehicles.filter(vehicle => vehicle.service_type == 'Replacement');
    }
}

Hope, this will solve your issue
Thanks,

I donā€™t understand your question, sorry.

Hi ,Sorry for the inconvenience, my requirement is i need to do date filtering using predefined/custom pipes ā€¦
in all the example i refer , they just doing business logic in pipe, and using that pipe at view/HTML level ,like this {{ date | ā€˜yyyy MM ddā€™}} ā€¦etcā€¦ my need is same thing how can we do in controller (ts file) under some methodā€¦how i can i call that pipe ,please helpā€¦

Thanks,
Hash57

Define your sorting function, then sort your array into sortedDates or something, then in the template use *ngFor over sortedDates. I do this with Observables, so the Observable emits a new value each time the sorted array changes and this new array value gets rendered in the template.

I only think of Pipes as a mask. The data is already computed, and I want to show only two decimal places, or all capital letters, something like that. So a Pipe performs extremely simple calculation, nothing hard.

this is my pipe

the idea is to call this pipe from controller side ,to get required format like we do in java,js functionsā€¦

import { Pipe, PipeTransform } from ā€˜@angular/coreā€™;
import { DatePipe } from ā€˜@angular/commonā€™;

@Pipe({
name: ā€˜custom-dateā€™,
})
export class CustomDatePipe implements PipeTransform {
result: string;
transform(value: Date, requiredFormat) {
var datePipe = new DatePipe(ā€œen-USā€);
this.result = datePipe.transform(value, ā€˜yyyyā€™);
return this.result;
}
}

in my controller file, i imported this pipe, DatePipe like this ,

import { CustomDatePipe } from ā€˜./ā€¦/ā€¦/pipes/custom-date/custom-dateā€™;
import { DatePipe } from ā€˜@angular/commonā€™;

and in the constructor,i added CustomDatePipe like this,
constructor(public customDate:CustomDatePipe,ā€¦){
}

at some point iā€™m calling like this

var result = this.customDate.transform(new Date(), ā€˜h aā€™);ā€¦
getting errors like

You may need an appropriate loader to handle this file type.
| this.customDate = customDate;

please check where iā€™m committed wrong,please help

Thanks,
Hash57

I donā€™t know anything about the Angular date pipe, because I use date-fns. But why do you need a special pipe for what you are doing? Why canā€™t you just use date:'MM-dd-yyyy' ?

My Requirement is iā€™m getting start_time:ā€œ20:00ā€ , end_time:ā€œ11:30ā€ values from server response, there is some more big response ,but here it is not required ā€¦ i need to show 8 am to 9pm ,if start,end_times are in different meridian ,vice versa (pm- am) , both the of them are in same case then 8 to 9 am and vice versa (pm)ā€¦ so i need to check them in which meridian they are so iā€™m trying to do.

in angularjs(1) i did like this for web app.

vm.matchStartTimeString = $filter(ā€˜dateā€™)(vm.matchStartTiming, ā€˜h aā€™);

the result of vm.matchStartTimeString will be like ===> 8 amā€¦
same thing iā€™m trying to do in native apps using ionicā€¦

and i make into 4 branches (both AM,AM-PM,PM-AM,both PM) , for selected state, in switch cases iā€™m doing bussines logicā€¦so, i unable to use that pipe at HTML level, i must use that pipe at controller level onlyā€¦

any help ,much appriciated

Thanks,
Hash57

Anything you need to do with dates can be done with date-fns.