我很惊讶这个问题没有被问到,所以也许我忽略了明显的问题.我有一个表格字段应该是一个数字.它的起始值为null,但是一旦输入并清除了一个数字,它就是一个空字符串.看起来JavaScript像数字一样对待"0".
所以,而不是说......
if ((this.RetailPrice != null && this.RetailPrice != 0) || this.RetailPrice === 0) {
return this.RetailPrice;
}Run Code Online (Sandbox Code Playgroud)
有没有办法将TypeScript数字类型扩展为具有IsNullOrEmpty()方法?或类似的东西会简化这个表达式?
最终,我想我正在寻找一些简单的东西......
if (this.RetailPrice) {
}
Run Code Online (Sandbox Code Playgroud)
Par*_*eva 18
你可以简单地使用typeof.它还将检查undefined,null,0和"".
if(typeof RetailPrice!='undefined' && RetailPrice){
return this.RetailPrice;
}
Run Code Online (Sandbox Code Playgroud)
Asa*_*ena 11
也排除空字符串
if(this.retailPrice && this.retailPrice.trim()){
//Implement your logic here
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48995 次 |
| 最近记录: |