【问题标题】:how to Use element read by ngfor as function parameter如何使用 ngfor 读取的元素作为函数参数
【发布时间】:2019-09-22 01:23:06
【问题描述】:

我有这种角度和平的代码:

<table>
        <tr>
            <th style="width:50%"><b>List rooms </b></th>
            <th></th>
        </tr>

        <tr *ngFor="let room of list_room">
            <td><b> {{room}}</b> </td>
            <td><button (click)="joinRoom()">JOIN</button></td>

        </tr>
</table>

我需要获取 {{room}} 的字符串内容并将其作为参数传递给函数 joinRoom()。

我试过这个链接:Send *ngFor value as parameter on click

我将代码更改为:

<td><button [(ngModel)]="room" (click)="joinRoom(room)">JOIN</button></td>

但我有一个编译错误。

我该怎么办?

【问题讨论】:

  • 不确定但试试这个:&lt;td&gt;&lt;button (click)="joinRoom({{room}})"&gt;JOIN&lt;/button&gt;&lt;/td&gt;

标签: html angular ngfor


【解决方案1】:

您需要删除 ngModel。

使用它应该可以工作:

<button (click)="joinRoom(room)">JOIN</button></td>

示例:https://stackblitz.com/edit/angular-xqnent

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多