[英]Angular reactive nested forms
我试图在我的 angular 项目中嵌套多个反应形式,并且形式在不同的组件中。
例如,我有一个组件,其中包含一个具有两个输入的表单,一个输入名称,一个输入描述和一个提交按钮。 我称这个组件为NameDescComponent
我计划在多个页面和表单中使用这个组件。 这是组件的 html。
<form [formGroup]="nameDescForm" (ngSubmit)="customEmit()">
<div fxLayout="row" fxLayoutGap="10px" fxFlex>
<mat-form-field>
<input matInput placeholder="Name" formControlName="name">
</mat-form-field>
<mat-form-field fxFlex>
<input matInput placeholder="Description" formControlName="description">
</mat-form-field>
</div>
<div fxLayout="column" fxLayoutGap="10px">
<button type="submit" mat-raised-button color="primary">
{{buttonText}}
</button>
<div>
</div>
</div>
</form>
这是缩写的 ts 文件
public nameDescForm: FormGroup;
@Input() public buttonText: string;
@Output() public save: EventEmitter<any> = new EventEmitter<any>();
@Output() public nameDescFormEmit: EventEmitter<FormGroup> = new EventEmitter<FormGroup>();
constructor(fb: FormBuilder) {
this.nameDescForm = fb.group({
'name': ['', Validators.required],
'description': ['']
});
}
public ngOnInit() {
console.log(this.nameDescForm);
this.nameDescFormEmit.emit(this.nameDescForm);
}
public customEmit() {
this.save.emit();
}
然后在我使用这个组件的页面中,我还有另一个表单, NameDescComponent
内有NameDescComponent
,就像这样
<form [formGroup]="parentForm" (ngSubmit)="customEmit()">
<app-name-description (nameDescFormEmit)="getNameDescForm($event)" buttonText="Save" (save)="save()"></app-name-description>
<input type="test" formControlName="test">
</form>
目前我正在做的是将nameDescFrom
从其组件传递给具有输出和 EventEmitter 的ParentComponent
。 此解决方案似乎有效,当我更新 child 时,我可以访问这些值。 但缺点是当我提交表单时,我必须检查parentForm
和nameDescFrom
是否都有效并分别管理两个表单。
我想知道是否有更好的方法来解决这个问题? 我什么时候可以从父表单中访问nameDescFrom
?
谢谢
要将表单与嵌套表单合并并对其进行单个验证,可以使用formbuilder
在根表单组件中创建整个模型对象结构。 然后,在其html模板中,将添加子表单的自定义元素(例如: <nested-form>
),该元素将呈现子表单。
参见示例: https : //stackblitz.com/edit/angular-m5fexe )
有用的角度文档链接:
代码:
export class Form1Component {
@Input() name: string;
public dummyForm: FormGroup;
constructor(
private _fb: FormBuilder,
) {
this.createForm();
}
createForm() {
this.dummyForm = this._fb.group({
username: ['username', Validators.required],
nestedForm: this._fb.group({
complement1: ['complement1', Validators.required],
complement2: ['complement2', Validators.required],
})
});
}
submit() {
if (this.dummyForm.valid) {
console.log('form AND subforms are valid', this.dummyForm.value);
} else {
console.warn('form AND/OR subforms are invalid', this.dummyForm.value);
}
}
}
Form1Component的模板:
<form [formGroup]="dummyForm" (ngSubmit)="submit()">
<div>
<label for="username">Root Input</label>
<input type="text" id="username" formControlName="username"/>
</div>
<nested-form [parentForm]="dummyForm"></nested-form>
<button>Send</button>
</form>
嵌套的表单代码:
export class NestedFormComponent {
@Input()
public parentForm: FormGroup;
}
嵌套表单模板:
<form [formGroup]="parentForm">
<div formGroupName="nestedForm">
<div>
<label for="complement1">Nested input 1</label>
<input type="text" formControlName="complement1"/>
</div>
<div>
<label for="complement1">Nested input 1</label>
<input type="text" formControlName="complement2"/>
</div>
</div>
</form>
您还可以使用自定义表单控件,它是 angular 的内置概念。
这个想法是有一个自定义组件,它将作为父表单和嵌套表单之间的“桥梁”。 自定义组件将侦听嵌套表单的“更改”事件并用它更新其值。 父表单将自定义控件视为单个字段,尽管其值将是一个包含嵌套表单控件值的对象。
自定义组件应该实现一个名为 ControlValueAccessor 的接口,通过该接口,angular 提供更新自定义控件状态(例如值、有效、触摸、原始等)的所有必要条件。 通过这种方式,您实际上可以管理自定义控件状态,以便父表单可以像对待任何其他表单控件一样对待它。
您可以在以下链接中阅读它:
https://blog.sreyaj.dev/custom-form-controls-controlvalueaccessor-in-angular
https://medium.com/@majdasab/implementing-control-value-accessor-in-angular-1b89f2f84ebf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.