Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 pipes under condition, check type

I found the way pipe under condition here

How can I check the type is number and give it pipes?

{{(item).isNumber ? (item | currency: 'USD':true:'1.2-2') : (item)}}

like this.

ps. I'd not like to use custom pipe decoration.

Any good ideas? Thanks

like image 648
Daisy Avatar asked Feb 21 '26 13:02

Daisy


1 Answers

You can only use methods and objects that are available from within your component class. So no native Javascript functions are available in the string interpolation.

You can however write a helper method in your component (taken from this post):

isNumber(o): boolean {
  return ! isNaN (o-0) && o !== null && o !== "" && o !== false;
}

And use it like this:

{{ isNumber(item) ? (item | currency: 'USD':true:'1.2-2') : (item) }}
like image 153
rinukkusu Avatar answered Feb 23 '26 05:02

rinukkusu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!