如何在Angular中处理空值 [英] How to handle null values in Angular
本文介绍了如何在Angular中处理空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将数据发送到Angular,但记录的值(截止日期)之一为空.我如何在Angular中处理此问题,以便如果value为null,则将其更改为-
I am sending data to Angular but one of the value (closed date) for a record is null. How can I handle this in Angular so that if value is null , it changed to --
getDetails() {
this.myService.getFlowerDetails(this.flowerId, this.flowerName).subscribe(
res => {
this.flower = res;
if(this.flower !== undefined){
this.flowerName = this.flower.flowerName;
this.flowerId = this.flower.flowerId.toString();
this.flowerCategory = this.flower.flowerCategory;
this.recordCreateDate = this.flower.recordCreateDate.toString();
this.recordClosedDate = this.flower.recordClosedDate.toString(); // this is null in the backend
}
else{
this.flowerName = "--";
this.flowerId = "--";
this.flowerCategory = "--";
this.recordCreateDate = "--";
this.recordClosedDate = "--";
}
}, er => {
this.throwError = er;
});
我收到以下错误.这不是整个堆栈跟踪(不确定是否需要)
I get the following error. This is not the entire stack trace (not sure if its needed)
TypeError
columnNumber: 17
fileName: "http://localhost:4200/main.bundle.js"
lineNumber: 1738
message: "_this.flower.recordClosedDate is null"
推荐答案
在 this.flower.recordClosedDate 的情况下,
||
运算符可用于设置默认值.code>是虚假的( undefined
, null
,空字符串,零):
The ||
operator can be used to set a default value, in cases where this.flower.recordClosedDate
is falsy (undefined
, null
, empty string, zero):
this.recordClosedDate = (this.flower.recordClosedDate || "--").toString();
这篇关于如何在Angular中处理空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文