Angular - 如何在插值中编写条件?
Angular - How can I write a condition in interpolation?
我有一个 table 正在通过 add client
表单填充。它工作正常并显示更改。问题是我有一个 select 列表,其中用户 select 是特定来源,然后将其保存在 ngmodel 中。这是代码。
Select 列表
<mat-select [(ngModel)]="model.source" name="source" placeholder="Select Source ">
<mat-option [value]="1 ">Upwork</mat-option>
<mat-option [value]="2 ">Refer from Friend</mat-option>
<mat-option [value]="3 ">Other</mat-option>
</mat-select>
现在在我的 table 中显示的值是基于 selection 的 1 或 2 或 3。我想写点东西,像这样插值的条件
Table 字段
<ng-container matColumnDef="source">
<mat-header-cell *matHeaderCellDef> Source </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.source ? if value is 1 : display (upwork), if value is 2 : display(refer from friend)}} </mat-cell>
</ng-container>
像这样的东西,我很喜欢angularjs
我不确定Angular
如果
你可以使用嵌套三元
{{element.source == 1 ? 'upwork' : (element.source == 2 ? 'refer from friend' : '')}}
或可能更好
export class MyComponent {
sourceNames = {1: 'upwork', 2: 'refer from friend', 3: 'other' };
}
{{sourceNames[element.source]}}
我有一个 table 正在通过 add client
表单填充。它工作正常并显示更改。问题是我有一个 select 列表,其中用户 select 是特定来源,然后将其保存在 ngmodel 中。这是代码。
Select 列表
<mat-select [(ngModel)]="model.source" name="source" placeholder="Select Source ">
<mat-option [value]="1 ">Upwork</mat-option>
<mat-option [value]="2 ">Refer from Friend</mat-option>
<mat-option [value]="3 ">Other</mat-option>
</mat-select>
现在在我的 table 中显示的值是基于 selection 的 1 或 2 或 3。我想写点东西,像这样插值的条件
Table 字段
<ng-container matColumnDef="source">
<mat-header-cell *matHeaderCellDef> Source </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.source ? if value is 1 : display (upwork), if value is 2 : display(refer from friend)}} </mat-cell>
</ng-container>
像这样的东西,我很喜欢angularjs
我不确定Angular
如果
你可以使用嵌套三元{{element.source == 1 ? 'upwork' : (element.source == 2 ? 'refer from friend' : '')}}
或可能更好
export class MyComponent {
sourceNames = {1: 'upwork', 2: 'refer from friend', 3: 'other' };
}
{{sourceNames[element.source]}}