英文:
Adding dynamic controls in an Angular form fails
问题
I am spending too much time on something that should be simpler, and I don't get where the mistake is. I just try to add some dynamic inputs controls to a form but it fails with the error Cannot find control with name: '0'
. I put an example here: Stackblitz demo
import { Component } from '@angular/core';
import {
FormArray,
FormBuilder,
FormControl,
FormGroup,
Validators,
} from '@angular/forms';
@Component({
selector: 'example',
templateUrl: 'example.html',
})
export class Example {
public form!: FormGroup;
constructor(private formBuilder: FormBuilder) {}
ngOnInit(): void {
this.form = this.formBuilder.group({
dependentsControls: new FormArray([]),
});
}
get formControls() {
return this form controls.
}
dependentsControls(): FormArray {
return
}
public createDependent() {
return this.formBuilder group({
dependentNameControl: new FormControl<string | undefined>('', [
Validators.minLength(2),
Validators.maxLength(50),
]),
});
}
public onAddDependent(): void {
this dependentsControls push(this createDependent).
console.log(this dependentsControls controls.
}
}
Copyright 2022 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at https://angular.io/license.
英文:
I am spending to much time on something that should be simpler and I don't get it where is the mistake. I just try to add some dynamic inputs controls to a form but it fails with error Cannot find control with name: '0'
. I put an example here:
Stackblitz demo
<form [formGroup]="form" class="top15x2" autocomplete="off">
<div class="w-100 text-start">
<button type='button' mat-button (click)="onAddDependent()" class="button-submit" style="width:150px">+ Add Dependent</button>
</div>
<div
*ngFor="let dependentsGroup of this.dependentsControls().controls let i=index"
[formGroupName]="i">
<div class="d-flex flex-row w-100 justify-content-start gap20">
<div class="col-2">
<mat-form-field class="w-100">
<mat-label>Name </mat-label>
<input matInput formControlName="dependentNameControl" />
<mat-error *ngIf="formControls['dependentNameControl'].invalid">
{{formControls['dependentNameControl'].hasError('minlength') ? "Min 2
chars":""}}
{{formControls['dependentNameControl'].hasError('maxlength') ? "Max 50
chars":""}}
</mat-error>
</mat-form-field>
</div>
</div>
</div>
</form>
<!-->
import { Component } from '@angular/core';
import {
FormArray,
FormBuilder,
FormControl,
FormGroup,
Validators,
} from '@angular/forms';
/** @title Basic datepicker */
@Component({
selector: 'example',
templateUrl: 'example.html',
})
export class Example {
public form!: FormGroup;
constructor(private formBuilder: FormBuilder) {}
ngOnInit(): void {
this.form = this.formBuilder.group({
dependentsControls: new FormArray([]),
});
} //ngOnInit
get formControls() {
return this.form.controls;
}
dependentsControls(): FormArray {
return <FormArray>this.form.controls['dependentsControls'];
}
public createDependent() {
return this.formBuilder.group({
dependentNameControl: new FormControl<string | undefined>('', [
Validators.minLength(2),
Validators.maxLength(50),
]),
});
}
public onAddDependent(): void {
this.dependentsControls().push(this.createDependent());
console.log(this.dependentsControls().controls);
}
}
/** Copyright 2022 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at https://angular.io/license */
答案1
得分: 2
把formArrayName
添加进去,并且将动态组的控制引用更改如下:
尝试这样做:
<div formArrayName="dependentsControls">
<div *ngFor="let dependentsGroup of this.dependentsControls().controls; let i = index" [formGroupName]="i">
<!-- ... -->
<mat-error *ngIf="dependentsGroup.get('dependentNameControl')?.invalid">
{{ dependentsGroup.get('dependentNameControl')?.hasError('minlength') ? "最少2个字符" : "" }}
{{ dependentsGroup.get('dependentNameControl')?.hasError('maxlength') ? "最多50个字符" : "" }}
</mat-error>
</mat-form-field>
</div>
</div>
英文:
You need to add formArrayName
, and also change controls reference to each dynamic group:
Try this:
<div formArrayName="dependentsControls">
<div
*ngFor="let dependentsGroup of this.dependentsControls().controls let i=index"
[formGroupName]="i"
>
...
<mat-error
*ngIf="dependentsGroup.get('dependentNameControl')?.invalid"
>
{{dependentsGroup.get('dependentNameControl')?.hasError('minlength')
? "Min 2 chars":""}}
{{dependentsGroup.get('dependentNameControl')?.hasError('maxlength')
? "Max 50 chars":""}}
</mat-error>
</mat-form-field>
</div>
</div>
</div>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论