5 typescript angular angular-reactive-forms angular8
将子组件表单放入父组件表单的最佳方法是什么?我们使用的是 2019 年最新的 Angular 8。经过研究,以下方法不能完全奏效。
父组件:
ngOnInit() {
this.parentForm = this.fb.group({
childForm1: etc
})
Run Code Online (Sandbox Code Playgroud)
子组件:
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 })
Run Code Online (Sandbox Code Playgroud)
}
方法一:
这个方法,https: //itnext.io/partial-reactive-form-with-angular-components-443ca06d8419 经过严格测试表明 ParentForm 有效,即使 Child Form 无效。这不应该发生。
ngOnInit() {
this.parent = this.fb.group({
fullName: null
})
Run Code Online (Sandbox Code Playgroud)
}
formInitialized(name: string, form: FormGroup) {
this.checkoutForm.setControl(name, form);
}
Run Code Online (Sandbox Code Playgroud)
方法二:
方法 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);
}
Run Code Online (Sandbox Code Playgroud)
那么 Angular 8 中最好的 Angular 官方实践是什么?
我根据您的问题创建了小场景。
父组件:
HTML:
<form [formGroup]="form">
<app-child [form]="form"></app-child>
<pre>{{form.value | json}}</pre>
</form>
Run Code Online (Sandbox Code Playgroud)
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)]],
})
}
}
Run Code Online (Sandbox Code Playgroud)
子组件:
HTML:
<div [formGroup]="form">
<input formControlName="streetNumber"><br>
<input formControlName="streetType"><br>
<input formControlName="city"><br>
<input formControlName="state"><br>
<input formControlName="postalCode">
</div>
Run Code Online (Sandbox Code Playgroud)
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() {
}
}
Run Code Online (Sandbox Code Playgroud)
工作链接:https : //stackblitz.com/edit/angular-gjrphg
多个孩子的更新链接:https : //stackblitz.com/edit/angular-svnqfh?file=src/app/app.component.html
| 归档时间: |
|
| 查看次数: |
7925 次 |
| 最近记录: |