user3463554 user3463554 - 10 months ago 42
TypeScript Question

Can I call an Angular2 pipe from a pipe?

I get back from the server an object containing an array of columns with their definitions and an array of rows. I would like to iterate over the columns and rows as I build my HTML table and format the cells according to the column type returned using a "format" pipe.

For example

WS Response:

"columns" [{"precision":10,"name":"PAGES","typeName":"INTEGER","scale":0...,
"rows":[{"PAGES":6531....}, [{"PAGES":6531....}]

HTML fragment:

<tr *ngFor="let row of invoices?.rows">
<td *ngFor="let column of invoices?.columns>
{{row[] | format : column}}

Is there any way my "format" pipe can just act a a delegator to the correct built-in pipe (where one exits), depending on the type of the column? I don't want to have to re-implement DecimalPipe, DatePipe, etc. etc.

Answer Source

Yes you can call pipes - just instatiate them and call transform:

transform(cell: any, column: any): any {
    if (column.typeName === "INTEGER") {
        let pipe = new DecimalPipe();
        return pipe.transform(cell, "1.0-0");
    // more here ...