Vicheanak Vicheanak - 10 months ago 262
AngularJS Question

How Can I Check isNaN in Angular2 Expression?

When I'm trying to do this:

dividerColor="{{isNaN(+price) ? 'warn' : 'primary'}}"

It gives me this error:

browser_adapter.ts:82 ORIGINAL EXCEPTION: TypeError: self.context.isNaN is not a function

How can I check if my object is a not number in Angular2 expression?

Answer Source

This happens because the expressions are evaluated against view context, not with eval, JS globals are unavailable in templates.

Considering that Number.isNaN was polyfilled, technically it can be done with

{{ (+price).constructor.isNaN(+price) ... }}

Though it can be considered a hack. I would suggest to expose each and every helper function as either class property or a pipe.