I want to compare angular 2 expressions. I got array of data in numeric string from my database i need to convert this to number before give a condition with ngClass for styling. how can i convert this type. any idea?
<td [ngClass]= "{'positive': gbh.Last > 0, 'negative': gbh.Last < 0}"> {{gbh.Last}} </td>
I want to compare angular 2 expressions. I got array of data in numeric string from my database i need to convert this to number before give a condition with ngClass for styling. how can i convert this type. any idea?
<td [ngClass]= "{'positive': gbh.Last > 0, 'negative': gbh.Last < 0}"> {{gbh.Last}} </td>
Share
Improve this question
asked Mar 28, 2017 at 12:03
U rockU rock
7752 gold badges15 silver badges37 bronze badges
3
- stackoverflow.com/questions/21398319/… – Faraz Babakhel Commented Mar 28, 2017 at 12:05
- 1 I'm not at my computer right now ... but does this work? +gbh.Last > 0 – DeborahK Commented Mar 28, 2017 at 17:11
- if array returns number it works exactly as i shown above. – U rock Commented Mar 29, 2017 at 7:41
2 Answers
Reset to default 9This answer is the typescript way: so answer
Number('1234') // 1234
Number('9BX9') // NaN
The other answer looks like it would be for int not numbers. You are better off using the plus symbol.
var x = "32";
var y = +x; // y: number
Ref answer:
As far as I know, you can not convert your numeric string to number inside the Angular expression.
The best practice will be to define a method in your controller which will solve your expression by using 'parseInt'
function isPositive (x: String, y: number): boolean {
return (parseInt(x, 10) < y);
}