在ngFor中定义临时变量

时间:2018-03-08 01:00:55

标签: angular angular5 angular-template

在我的angular 5模板中,我创建了如下表格行:

<tr *ngFor="let site of auditedLabNames">
    <th>{{ site }}</th>
    <th>{{ subnetRemediationBySite[site].Mapped }}</th>
    <th>{{ subnetRemediationBySite[site].Count }}</th>

我将在此行中多次使用该subnetRemediationBySite[site].Mapped值。有没有什么方法可以将它分配给变量,所以我不必每次输出整件事情?

1 个答案:

答案 0 :(得分:0)

您可以尝试将其循环到您的组件中,然后将其传递给另一个更短的&#39;变量

export class YourComponent{
     public data: any = [];

     constructor(){
        // Your subnetRemediationBySite data somewhere here...

        auditedLabNames.map(res => {
             this.data.push({
                 key1: res,
                 key2: subnetRemediationBySite[res].Mapped,
                 key3: subnetRemediationBySite[res].Count
             });
        })
     }
}

在您的模板中,您可以将其循环为...

<tr *ngFor="let site of data">
<th>{{ site.key1 }}</th>
<th>{{ site.key2 }}</th>
<th>{{ site.key3 }}</th>

您可以根据代码结构调整我的代码。只是明白了......希望这会有所帮助...