输入在编辑值时失去焦点.使用 ngFor 和 ngModel.角5 [英] input loses focus when editing value. Using ngFor and ngModel. Angular5

查看:22
本文介绍了输入在编辑值时失去焦点.使用 ngFor 和 ngModel.角5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的列表,它以双向方式绑定到 hero.component.ts 中的参数 hero.

I have a simple list that binds two-way to a parameter hero in hero.component.ts.

虽然当我开始在输入字段中输入时它失去焦点,我必须再次单击它.我应该怎么做才能允许用户编辑输入值而不会使输入字段丢失目标?

Though when I start typing in the input field it loses focus and I have to click it again. What am I supposed to do in order to allow the user edit the input value without the input field loses target?

 <li *ngFor="let power of hero.powers; let i = index">
      <input [(ngModel)]="hero.powers[i]"/>
 </li>

推荐答案

使用 angular 的 trackBy 函数修复问题.

Using trackBy function of angular fixes the issue.

演示示例

这篇关于输入在编辑值时失去焦点.使用 ngFor 和 ngModel.角5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆