gpt4 book ai didi

Angular 在父组件嵌套表单中使用子组件表单

转载 作者:行者123 更新时间:2023-12-03 16:03:23 38 4
gpt4 key购买 nike

将子组件表单放入父组件表单的最佳方法是什么?我们使用的是 2019 年最新的 Angular 8。经过研究,以下方法不能完全奏效。

父组件:

 ngOnInit() {
this.parentForm = this.fb.group({
childForm1: etc
})

子组件:
this.ChildForm = this.formBuilder.group({
'streetNumber': [null, [Validators.required, Validators.maxLength(32)]],
'streetType': [null, [Validators.maxLength(8)]],
'city': [null, [Validators.maxLength(32)]],
'state': [null, [Validators.maxLength(16)]],
'postalCode': [null, [Validators.maxLength(16)]],
}, { validator: atLeastOneLocationRequired })

}

方法一:

这个方法, https://itnext.io/partial-reactive-form-with-angular-components-443ca06d8419
经过严格的测试表明 ParentForm 有效,即使 Child Form 无效。这不应该发生。
ngOnInit() {
this.parent = this.fb.group({
fullName: null
})

}
formInitialized(name: string, form: FormGroup) {
this.checkoutForm.setControl(name, form);
}

方法二:

方法 2 使用 ViewChild,这是听力不好的做法。
https://davembush.github.io/attaching-an-angular-child-component-s-form-to-a-parent/
@ViewChild(ChildComponent) childComponent: ChildComponent;

And now in ngAfterViewInit() we can add the child’s FormGroup as an additional “control” and set the parent FormGroup to the parent control’s FormGroup.

ngAfterViewInit() {
this.form.addControl('childForm', this.childComponent.form);
this.childComponent.form.setParent(this.form);
}

那么 Angular 8 中最好的 Angular 官方实践是什么?

最佳答案

我根据您的问题创建了小场景。

父组件:

HTML:

<form [formGroup]="form">
<app-child [form]="form"></app-child>
<pre>{{form.value | json}}</pre>
</form>

TS:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
form: FormGroup;
constructor (private fb: FormBuilder) {}

ngOnInit() {
this.form = this.fb.group({
childForm1: '',
streetNumber: [null, [Validators.required, Validators.maxLength(32)]],
streetType: [null, [Validators.maxLength(8)]],
city: [null, [Validators.maxLength(32)]],
state: [null, [Validators.maxLength(16)]],
postalCode: [null, [Validators.maxLength(16)]],
})
}
}

子组件:

HTML:
<div [formGroup]="form">
<input formControlName="streetNumber"><br>
<input formControlName="streetType"><br>
<input formControlName="city"><br>
<input formControlName="state"><br>
<input formControlName="postalCode">
</div>

TS:
import { Component, OnInit, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';

@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Input() form: FormGroup;
constructor() { }

ngOnInit() {
}

}

工作链接: https://stackblitz.com/edit/angular-gjrphg

更新多个 child 的链接: https://stackblitz.com/edit/angular-svnqfh?file=src/app/app.component.html

关于Angular 在父组件嵌套表单中使用子组件表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59302396/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com