简体   繁体   English

Angular - 如何在插值中写入条件?

[英]Angular - How can I write a condition in interpolation?

I have a table which is being populated through the add client form. 我有一个表通过add client表单填充。 It works fine and the changes are displayed. 它工作正常,并显示更改。 The thing is I have a select list in which a user selects the specific source and then it is saved in ngmodel. 问题是我有一个选择列表,用户在其中选择特定的源,然后将其保存在ngmodel中。 Here is the code. 这是代码。

Select List 选择列表

 <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>

Now in my table the value which displays is 1 or 2 or 3 based on the selection. 现在在我的表中,显示的值基于选择显示为1或2或3。 I want to write something, a condition in interpolation like this. 我想写一些东西,像这样的插值条件。

Table Field 表场

<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>

Some thing like this, I did like it in angularjs I am not sure about Angular 像这样的事情,我确实喜欢angularjs我不确定Angular

You can use nested ternary if 您可以使用嵌套的三元组

{{element.source == 1 ? 'upwork' : (element.source == 2 ? 'refer from friend' : '')}}

or probably better 或者可能更好

export class MyComponent {
  sourceNames = {1: 'upwork', 2: 'refer from friend', 3: 'other' };
}
{{sourceNames[element.source]}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM