gpt4 book ai didi

Angular 将 FormGroup 添加到 FormGroup

转载 作者:行者123 更新时间:2023-12-03 15:30:53 26 4
gpt4 key购买 nike

我有一个父组件,它是主要的 FormGroup .在此 <my-parent></my-parent>组件我有一个子组件,<my-child></my-child> .

子组件有一个 *ngIf="some-conditional" ,在子组件内部,我有一个快速填充按钮,它基本上是 patchValue在一对夫妇
child 的FormGroup控件。

它成功添加了 child FormGroup给 parent FormGroup ,但父级内部子级的值始终为空。

我尝试通过两种方式(均不成功)添加控件,在父组件中使用 setter,从子组件内部发出并将新值重新分配给父 FormGroup .

这是一个 stackblitz带有我尝试做的演示。提前致谢!

// PARENT COMPONENT

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

import {
ChildComponent
} from './child.component';

@Component({
selector: 'my-parent',
templateUrl: './parent.component.html',
styleUrls: ['./app.component.scss']
})
export class ParentComponent {
isChildFormSet = false;
showChildComponent = false;
exampleParentForm: FormGroup;

// @ViewChild(ChildComponent)
// set userForm(childForm: ChildComponent) {
// if (childForm && !this.isChildFormSet) {
// this.isChildFormSet = true;
// setTimeout(() => {
// this.exampleParentForm.addControl('child', new FormGroup(childForm.exampleChildForm.controls));
// });
// }
// }

constructor(private fb: FormBuilder) {
this.exampleParentForm = this.fb.group({
parent: ['', Validators.required]
})
}

submitForm(): void {
console.log(this.exampleParentForm)
}

addChildComponent(): void {
this.showChildComponent = true;
}

onChange(form) {
// reset the form value to the newly emitted form group value.
this.exampleParentForm = form;
}
}


// CHILD COMPONENT

import {
Component,
OnInit,
Input,
Output,
EventEmitter
} from '@angular/core';
import {
FormBuilder,
FormGroup,
Validators
} from '@angular/forms';

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

@Input() formData: FormGroup;
@Output() onFormGroupChange: EventEmitter < FormGroup > = new EventEmitter < FormGroup > ();


constructor(private fb: FormBuilder) {}

ngOnInit() {
this.exampleChildForm = this.fb.group({
child: ['', Validators.required]
});

this.addGroupToParent();
}

fillWithText() {
this.exampleChildForm.patchValue({
child: 'This is an exmple.'
});
}

clearText(): void {
this.exampleChildForm.get('child').setValue('');
}

private addGroupToParent(): void {
this.formData.addControl('child', new FormGroup(this.exampleChildForm.controls));
this.onFormGroupChange.emit(this.formData);
}
}

<!-- PARENT COMPONENT -->

<div class="parent-container">
<form [formGroup]="exampleParentForm" (submit)="submitForm()" novalidate>
<div class="col-3">
<input class="effect-1" type="text" placeholder="Parent Input" formControlName="parent" />
<span class="focus-border"></span>
</div>
<my-child *ngIf="showChildComponent" [formData]="exampleParentForm" (onFormGroupChange)="onChange($event)">
</my-child>
<div>
<button class="button parent" type="submit">Log Form Value</button>
</div>
</form>
<button class="button small" type="button" (click)="addChildComponent()">Add Child Component</button>
</div>

<!-- CHILD COMPONENT -->

<div class="child-container" [formGroup]="exampleChildForm">
<div class="col-3">
<input class="effect-1" type="text" placeholder="Child Input" formControlName="child">
<span class="focus-border"></span>
</div>
<div>
<button class="button child" type="button" (click)="fillWithText()">Patch Value</button>
</div>
</div>

最佳答案

无需向父组添加新的 FormGroup 实例,只需添加子表单组本身即可。

所以

this.formData.addControl('child', new FormGroup(this.exampleChildForm.controls));

变成
this.formData.addControl('child', this.exampleChildForm);

Forked Stackblitz

关于Angular 将 FormGroup 添加到 FormGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54811764/

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