【问题标题】:I have dynamic id generated by ngFor, how can i change the value of the input?我有 ngFor 生成的动态 id,如何更改输入的值?
【发布时间】:2019-11-20 14:07:33
【问题描述】:

我正在用每一列动态加载一个表,行有一个唯一的 ID。如何在OnInit() 方法上动态更改单元格内的输入值?

这是我的html专栏

<td><input class="form-control" placeholder="Title" value=""
           id="title5{{i5}}" name="title5{{i5}}">
</td>

并尝试通过分配

for (let i = 0; i < rowsFinal.length; i++) {

    let tempArray: any[] = rowsFinal[i];

    const title5 = <HTMLInputElement>document.getElementById('title5' + i);
    title5.value = 'aaa';
    console.log('title5' + i + "=" + title5.value);
}

我的问题是我可以通过其 id 为输入分配任何值。

【问题讨论】:

    标签: html angular binding assign


    【解决方案1】:

    尝试在您的输入中使用 [(ngModel)],然后在 component.ts 文件中,当您更改该模型的值时,它将在输入中更新 示例:

    <tr *ngFor="let item of ListOfItems">
    <td>
    <input [(ngModel)]="item.SomeProperty"/>
    </td>
    </tr>
    

    然后在 .ts 文件中。

    this.ListOfItems[0].SomeProperty = "New Value"; //Use loop and condition for your specific object.
    

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2019-10-27
      • 2019-01-24
      • 1970-01-01
      相关资源
      最近更新 更多