简体   繁体   English

Angular 表单控件很快失去焦点

[英]Angular form controls lose focus very quickly

In some of my Angular Form controls, the focus loses very quickly, after about 2 seconds or 1 key-press.在我的一些 Angular Form 控件中,焦点会在大约 2 秒或 1 次按键后很快失去。 The forms where this happens are in divs with a *ngFor structure (see code below).发生这种情况的表单在带有 *ngFor 结构的 div 中(见下面的代码)。 How could I make this stop, so that someone can just fill in their entire answer at once?我怎么能让这个停止,以便有人可以一次填写他们的整个答案?

<div formArrayName="profiles" *ngFor="let profile of fillInForm.controls.profiles?.value; let i = index">
    <div [formGroupName]="i">
      <input type="text" formControlName="network" placeholder="Network">
      <input type="text" formControlName="username" placeholder="Username">
      <input type="text" formControlName="url" placeholder="URL">
    </div>
</div>

(I'm sorry if I have not given you enough information, I am new to this. Please tell me if you need to know more) (如果我没有给你足够的信息,我很抱歉,我是新手。如果你需要知道更多,请告诉我)

You shouldn't loop through the value like this fillInForm.controls.profiles?.value .你不应该像fillInForm.controls.profiles?.value这样循环遍历值。 The value is an object and this object will keep changing and render the element every time.该值是一个对象,该对象每次都会不断变化并呈现元素。 This will have the effect you described where the focus will be lost as it is a new element.这将产生您描述的效果,因为它是一个新元素,焦点将丢失。

So you have to loop through form controls .所以你必须遍历表单controls

<div formArrayName="profiles" *ngFor="let profile of fillInForm.get('profiles').controls; let i = index">
    <div [formGroupName]="i">
      <input type="text" formControlName="network" placeholder="Network">
      <input type="text" formControlName="username" placeholder="Username">
      <input type="text" formControlName="url" placeholder="URL">
    </div>
</div>

demo 🚀演示🚀

In some of my Angular Form controls, the focus loses very quickly, after about 2 seconds or 1 key-press.在我的某些Angular Form控件中,大约2秒钟或1次按键后,焦点很快消失了。 The forms where this happens are in divs with a *ngFor structure (see code below).发生这种情况的形式在具有* ngFor结构的div中(请参见下面的代码)。 How could I make this stop, so that someone can just fill in their entire answer at once?我该如何停止,以便某人可以一次填写整个答案?

<div formArrayName="profiles" *ngFor="let profile of fillInForm.controls.profiles?.value; let i = index">
    <div [formGroupName]="i">
      <input type="text" formControlName="network" placeholder="Network">
      <input type="text" formControlName="username" placeholder="Username">
      <input type="text" formControlName="url" placeholder="URL">
    </div>
</div>

(I'm sorry if I have not given you enough information, I am new to this. Please tell me if you need to know more) (很抱歉,如果我没有给您足够的信息,这是我的新手。请告诉我您是否需要了解更多信息)

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

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