I have tried everything on StackOverflow similar questions but still I couldn’t fix this issue.
I have this method in my component.ts file.
private initForm() {
if (this.form) {
this.form.patchValue(this.course);
} else {
var controlsConfig = {
duration: [this.course.duration],
credits: [this.course.credits],
expires: [],
comment: [this.course.comment]
};
// adding custom fields to course controls
for (var i = 0; i < this.courseCustomFields.length; i++) {
controlsConfig[this.courseCustomFields[i].key] = [this.course[this.courseCustomFields[i].key]];
}
this.form = this.formBuilder.group(controlsConfig);
}
And I have populated the custom field input fields in my component.html like below.
<div class="row" *ngFor="let courseCustomFieldRow of courseCustomFieldsForFront">
<div class="col-md-3" *ngFor="let courseCustomField of courseCustomFieldRow">
<div class="form-group">
<label>{{courseCustomField.title}}</label>
<input type="text" class="form-control" [formControlName]="courseCustomField.key">
</div>
</div>
</div>
But still it loses focus when I type one character.
2
Answers
I am not able to replicate your issue, below is a stackblitz for the same! try adding track by for the
ngFor
it might stop the lists from re-rendering!If your issue is not resolved please share back the stackblitz with the issue replicated!
ts
Stackblitz Demo
Generally this behavior is because you’re iterating over an array and the "input" change the array (so Angular must render it again). This is the reason, e.g. iterate in a FormArray over the formArray.controls, or use trackBy, or use an auxiliar array than not change (e.g. we can create
this.aux=this.array.map((x,index)=>index)
and iterate over "aux" instead of this.array. But I don’t know this case in your code 🙁Three more ideas:
I can not understand this
[
]
in your codeelse you’re assign an array! to the control value (if you have
an input tag has no sense)
NOT use "var" else "let" or "const". var create a variable global in your aplication, let
It’s possible you want to use a FormArray of FormGroups instead of
an array of FormGroups?