gpt4 book ai didi

angular - 如何在 Angular 中处理空值

转载 作者:行者123 更新时间:2023-12-02 16:09:32 24 4
gpt4 key购买 nike

我正在向 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"

最佳答案

如果 this.flower.recordClosedDate 为假(undefinednull,空字符串,零):

this.recordClosedDate = (this.flower.recordClosedDate || "--").toString();

关于angular - 如何在 Angular 中处理空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49562770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com