【发布时间】:2019-04-13 09:28:20
【问题描述】:
我想在值为NAN 时使用*ngIf* 隐藏标签,但它不起作用。
标记的标签是变量的默认值,一旦输入被填充,值将是一个数字
我只想在值不是NAN时显示标签
尝试了什么
// undefined
<mat-hint *ngIf="this.cost_liter !== 'undefined'" >cost/liter: {{this.cost_liter}}</mat-hint>
// 'undefined'
<mat-hint *ngIf="this.cost_liter !== 'undefined'" >cost/liter: {{this.cost_liter}}</mat-hint>
//!=
<mat-hint *ngIf="this.cost_liter != undefined" >cost/liter: {{this.cost_liter}}</mat-hint>
//!== NAN
<mat-hint *ngIf="this.cost_liter !== NAN" >cost/liter: {{this.cost_liter}}</mat-hint>
//!= NAN
<mat-hint *ngIf="this.cost_liter != NAN" >cost/liter: {{this.cost_liter}}</mat-hint>
//!== null
<mat-hint *ngIf="this.cost_liter !== null" >cost/liter: {{this.cost_liter}}</mat-hint>
// != null
<mat-hint *ngIf="this.cost_liter != null" >cost/liter: {{this.cost_liter}}</mat-hint>
我确信 ngIf 正在工作,因为如果值大于 0,我设置了一个条件。它可以工作,但仍然不是我的需要
// > 0
<mat-hint *ngIf="this.cost_liter != null" >cost/liter: {{this.cost_liter}}</mat-hint>
【问题讨论】:
-
NaN != NaN是 JavaScript 的一个有趣的小怪癖,所以你的比较总是会返回 true。
标签: angular if-statement angular-ng-if