我正在向 Angular 发送数据,但记录的其中一个值(关闭日期)为空。我如何在 Angular 中处理这个问题,以便如果 value 为 null ,它会更改为--
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;
});
我收到以下错误。这不是整个堆栈跟踪(不确定是否需要)
TypeError
columnNumber: 17
fileName: "http://localhost:4200/main.bundle.js"
lineNumber: 1738
message: "_this.flower.recordClosedDate is null"
The ||
运算符可用于设置默认值,在以下情况下this.flower.recordClosedDate
是假的(undefined
, null
,空字符串,零):
this.recordClosedDate = (this.flower.recordClosedDate || "--").toString();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)