Daisy
Daisy

Reputation: 89

Can I return built-in pipe?

I have a question about angular2 pipe. I want to get pipes as return value depending on schema:string. I think 2 way to get it, but both are not working.

page.html

<p>{{value | getSchema(value, schema)}}</p>

page.ts

getSchema(value, schema){
    if(schema == 'Currency'){
        return "currency: 'USD':true";
    } else if(schema == 'Number'){
        return 'number';
    }
}

or

page.html

<p>{{getSchema(value, schema)}}</p>

page.ts

getSchema(value, schema){
    if(schema == 'Currency'){
        return value + "| currency: 'USD':true";
    } else if(schema == 'Number'){
        return value + '| number';
    }
}

is there any ideas? Thanks.

Upvotes: 2

Views: 68

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657118

You can use a custom pipe that calls to another pipe depending on the parameter

@Pipe({name: 'genericPipe'})
class MyPipe {
  constructor(private currPipe:CurrencyPipe, private numberPipe:NumberPipe) {}
  transform(value, schema) {
    if(schema == 'Currency') {
      return this.currPipe.transform(value);
    } else {
      return this.numberPipe.transform(value);
    }
  }
}
<p>{{value | genericPipe:schema}}</p>

Upvotes: 2

Related Questions