* ngFor-在同一级别循环两个数组

时间:2018-09-21 10:03:57

标签: arrays angular ngfor

我正在尝试使用一个* ngFor循环两个数组,但是在浏览器上没有显示任何元素。我在下面的HTML文件示例中,也在组件文件中初始化了数组。

HTML文件:

<tbody *ngIf="feedbacks.length">
          <ng-container *ngFor="let fd of feedbacks;let res of results;let i=index">
            <tr>
              <td>{{ i }}</td>
              <td>{{fd}}</td>
              <td colspan="2">
                <ul>
                  <div *ngFor="let obj of res">
                    <td>Score: {{res.score}}</td>
                    <td>Emotion: {{res.tone_name}}</td>
                  </div>
                </ul>
              </td>
            </tr>
          </ng-container>
        </tbody>

打字稿文件:

  results = [];
  feedbacks = [];

  ngOnInit(){

    this.feedbacks = ["Le système est trop lent ", "Le top! Un exemple d'excellence.", "C'est quoi ce bordel !"]; 

    this.results = [
      [{score: 0.535632, tone_id: "anger", tone_name: "Colère"}],
      [{score: 0.633569, tone_id: "anger", tone_name: "Colère"},
       {score: 0.506763, tone_id: "analytical", tone_name: "Analytique"}],
      [{score: 0.895438, tone_id: "joy", tone_name: "Joie"}]  
    ];
    console.log(this.results);

  }

5 个答案:

答案 0 :(得分:4)

为此使用let obj of results[i]

<tbody *ngIf="feedbacks.length">
    <ng-container *ngFor="let fd of feedbacks;let i=index">
        <tr>
            <td>{{ i }}</td>
            <td>{{fd}}</td>
            <td colspan="2">
                <ul>
                    <div *ngFor="let obj of results[i]">
                        <td>Test: {{obj.score}}</td>
                    </div>
                </ul>
            </td>
        </tr>
    </ng-container>
</tbody>

答案 1 :(得分:2)

ngFor结构指令仅接受单个可迭代项作为输入。它不会接受多个数组。建议不要创建两个单独的数组,而要尝试遍历两个单独的数组(如果两个数组的长度相同)。

// HTML

<tbody *ngIf="feedbacks.length">
   <ng-container *ngFor="let item of combinedArray;let i=index">
     <tr>
       <td>{{ i }}</td>
       <td>{{fd}}</td>
         <td colspan="2">
           <ul>
             <div *ngFor="let obj of item.result">
               <td>Test: {{obj.score}}</td>
             </div>
           </ul>
       </td>
    </tr>
  </ng-container>
</tbody>

// Typescript

combinedArray: { feedback: any, results: any }[] = [];

ngOnInit(){
  ...

  this.feedbacks.forEach((fb, index)
    => this.combinedArray.push({ feedback: fb, result: this.results[index] }));
}

答案 2 :(得分:1)

您应该对模板进行一些修改:

<tbody *ngIf="feedbacks.length"> <ng-container *ngFor="let feedback of feedbacks;let i=index"> <tr> <td>{{ i }}</td> <td>{{feedback}}</td> <td colspan="2"> <ul> <div *ngFor="let result of results[i]"> <td>Test: {{result.score}}</td> </div> </ul> </td> </tr> </ng-container> </tbody>

这假定数组中的索引彼此对应。另一个解决方案是将模型修改为如下形式:

feedbacks = [{ value: "Le système est trop lent ", results: [{score: 0.535632, tone_id: "anger", tone_name: "Colère"}] }];

并使用两个嵌套的*ngFor进行迭代。

答案 3 :(得分:0)

<ng-container *ngFor="let fd of feedbacks;index as i">
<ng-container *ngFor="let res of results;index as j>
    <ng-container *ngIf="i===j">
    {{Here You can access fd and res of same index}}
    <ng-container>
</ng-container>
</ng-container>

在 angular11 上尝试过类似的代码,它工作正常。

答案 4 :(得分:0)

试试这个?

<tbody>
        <tr *ngFor="let fd of feedbacks;let i=index">
            <td>{{ i }} </td>
            <td>{{fd}} {{results[i].score}}</td>
            <td>{{fd}} {{results[i].tone_id}}</td>
            <td>{{fd}} {{results[i].tone_name}}</td>
        </tr>
</tbody>