如何替换 ngFor 中的 null 值
How to replace a value of null in ngFor
如果项目值为 null,则控制台中出现错误,它将停止 working.Hence如何检查或将值 null 替换为我的首选项?
this.xx = this.broadCastService.events.subscribe((line: Line) => {
this.configService.getConfig2()
.subscribe((e: LineVariantTrack) => {
this.segments = e.segments;
});
});
在模板中:
<tr *ngFor="let x of segments ">
<td>
<label>
{{x.from.latitude}} //Might be NULL
</label>
</td>
<td>
<label>
{{x.to.name}}
</label>
</td>
</tr>
如果值为 null
,您可以使用安全运算符 (?
) 和 ||
运算符来显示一些文本
大致如下:
<tr *ngFor="let x of segments ">
<td>
<label>
{{x?.from?.latitude || 'Not Available'}} //Might be NULL
</label>
</td>
<td>
<label>
{{x.to.name}}
</label>
</td>
</tr>
如果项目值为 null,则控制台中出现错误,它将停止 working.Hence如何检查或将值 null 替换为我的首选项?
this.xx = this.broadCastService.events.subscribe((line: Line) => {
this.configService.getConfig2()
.subscribe((e: LineVariantTrack) => {
this.segments = e.segments;
});
});
在模板中:
<tr *ngFor="let x of segments ">
<td>
<label>
{{x.from.latitude}} //Might be NULL
</label>
</td>
<td>
<label>
{{x.to.name}}
</label>
</td>
</tr>
如果值为 null
?
) 和 ||
运算符来显示一些文本
大致如下:
<tr *ngFor="let x of segments ">
<td>
<label>
{{x?.from?.latitude || 'Not Available'}} //Might be NULL
</label>
</td>
<td>
<label>
{{x.to.name}}
</label>
</td>
</tr>