简体   繁体   English

ng-repeat in angular 2

[英]ng-repeat in angular 2

Need to display a text area for displaying the details, once the details button is clicked. 单击详细信息按钮后,需要显示用于显示详细信息的文本区域。

It gives an output similar to this table structure 它提供类似于此表结构的输出

      <table>
        <th>ID</th>
        <th>Title</th>
        <tr *ngFor = "let row of indexes">
        <td *ngFor = "let id of row>{{id}}</td>
        <td><button (click)="showDetails()">DETAILS</td>
       </tr>
       </table>

<tr *ngIf="isClicked"><td>{{id.details}}</td></tr> ---> This needs to be displayed very next to the row where the button is clicked. <tr *ngIf="isClicked"><td>{{id.details}}</td></tr> --->这需要显示在单击按钮的行旁边。

In angular 1.x we can achieve this using ng-repeat-start/ng-repeat-end. 在角度1.x中,我们可以使用ng-repeat-start / ng-repeat-end实现此目的。 In angular 2 I have an clue of including </template ngFor let-row [ngForOf]="indexes"> But not sure where to include this. 在角度2我有一个包含</template ngFor let-row [ngForOf]="indexes">的线索但不知道在哪里包含这个。 Please suggest. 请建议。

If i understand your problem correctly, you want to insert two table-rows for each of your indexes . 如果我正确理解您的问题,您希望为每个indexes插入两个表行。

So if you have something like this in your component class: 所以,如果你的组件类中有这样的东西:

rows: any[] = [
    { detailsVisible: false },
    { detailsVisible: false },
    { detailsVisible: false },
    { detailsVisible: false },
    { detailsVisible: false }
];

toggleDetails(row: any) {
    row.detailsVisible = !row.detailsVisible;
}

You can do it by using ng-container : 您可以使用ng-container

<table>

    <ng-container *ngFor="let row of rows">
        <tr>
            <td (click)="toggleDetails(row)">show details</td>
        </tr>

        <tr *ngIf="row.detailsVisible">
            <td>only visible after click on details cell</td>
        </tr>
    </ng-container>

</table>

Hope this helps. 希望这可以帮助。

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

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