如何在另一个if条件中添加if条件

问题描述 投票:0回答:1

我是 Angular 17 的新手,我有一些基本问题。我声明的接口如下:

.ts

export interface Monthly {
  label?: string;
  description?: string;  
  quantity?: number;
}

然后我在类中声明了 Monthly 接口,如下所示:

monthly: Monthly = {
    label: 'Test label',
    description: "Test desc",
    quantity: 3,
  }

数量值在这里是硬编码的,但它可以改变。 下面是我声明的 const 并设置它的值。如何添加对 this.monthly.quantity 的检查,以便在其值大于 1 时与 this.monthly.label 连接,但在其值小于 2 时不显示。

const desc = this.monthly?.label ? `${this.monthly.quantity} ${this.monthly.label}` : ` No label`;
angular typescript
1个回答
0
投票

您可以再添加一个三元条件来执行此检查!

cosnt label = `${this.monthly.quantity}${this.monthly?.quantity === 1 ? ` ${this.monthly.label}` : ''}`;
const desc = this.monthly?.label ? label : ` No label`;
© www.soinside.com 2019 - 2024. All rights reserved.