最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert numeric string to number in angular 2 - Stack Overflow

programmeradmin2浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 9

This 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);
  }
发布评论

评论列表(0)

  1. 暂无评论