Daisy
Daisy

Reputation: 89

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

Upvotes: 3

Views: 3177

Answers (1)

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

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) }}

Upvotes: 4

Related Questions