【问题标题】:How to set "N/A" if data is null in angular如果数据在角度中为空,如何设置“N/A”
【发布时间】:2022-09-30 15:32:14
【问题描述】:

可以说我有一个表,有时很少有数据可以为空,但空数据在表中显示未定义而不是显示未定义我想显示“N/A”我如何才能实现共享图像和下面的代码。

在上图中的参考数据下,一些数据显示未定义,因为它为空,我希望它显示“N/A”。我将在下面分享代码。下面的代码有 \"a.normalRange\" 变量,该变量保存参考数据。

 var col = [\"Test Name\", \"Result\", \"Unit\" , \"Reference\"];
    this.hubxDataItemSpList.forEach((a) => {
      medicineInfo = this.hubxDataItemSpList.find(x => x.id == a.id);
      rows.push([\'\' + a.categoryName +\' \'+ a.itemTitle + \'\', \'\' + a.itemValue + \'\', \'\' + a.itemUnit ,\'\' + a.normalRange]);
    });

这是我的模特

export class HubxDataItemSpModel {
    id: number;
    categoryId: number;
    categoryName:string;
    itemTitle: string;
    itemUnit: string;
    isActive: boolean=true;
    itemValue: string;
    patientId: number;
    clinicianHubXSign : string;
    isAprrovedStatus : boolean;
    isTestApproved : boolean;
    phySignDate : Date;
    notes : string;
    normalRange : string;
    //base64:any;
    isDeleted: boolean;
    createdDate : Date
}

这就是我在组件类中定义模型的方式

hubxDataItemSpList : Array<HubxDataItemSpModel>=[];

这就是我打印表格的方式

 doc.autoTable({
            columnStyles: {
              0: { cellWidth: 45 },
              1: { cellWidth: 45 },
              2: { cellWidth: 45 },
              3: { cellWidth: 45 }
            },
            head: [col],
            body: rows,
            startY: 100,
            theme: \'plain\',
            tableLineColor: [242, 238, 238],
            tableLineWidth: 0.5,
            styles: {
              font: \'courier\',
              lineColor: [242, 238, 238],
              lineWidth: 0.5
            },
          });
  • 你能把你那行的html代码贴出来吗

标签: angular


【解决方案1】:

使用合并运算符,它测试 null 和 undefined。

{{ item.value ?? 'N/A' }}

如果您还希望将 0 和 '' 计为 N/A,则使用短路:

{{ item.value || 'N/A' }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多