gpt4 book ai didi

forms - 以 angular2 模型驱动形式重用组件

转载 作者:太空狗 更新时间:2023-10-29 17:09:44 26 4
gpt4 key购买 nike

我是 angular2 的新手,在过去的几天里,我一直在尝试使用模型驱动的表单创建可重用的表单组件

假设我们有一个组件 componentA.component.ts

@Component({
selector: 'common-a',
template: `
<div [formGroup]="_metadataIdentifier">
<div class="form-group">
<label>Common A[1]</label>
<div>
<input type="text" formControlName="valueA1">
<small>Description 1</small>
</div>
<div class="form-group">
<label>Common A[2]</label>
<div>
<input type="text" formControlName="valueA2">
<small>Description 2</small>
</div>
</div>
`
})


export class ComponentA implements OnInit{

@Input('group')
public myForm: FormGroup;

constructor(private _fb: FormBuilder) {
}

ngOnInit() {
this.myForm = this._fb.group({
valueA1 : ['', [Validators.required]],
valueA2 : ['', [Validators.required]],
});
}
}

还有一个组件B componentB.component.ts

@Component({
selector: 'common-b',
template: `
<div [formGroup]="_metadataIdentifier">
<div class="form-group">
<label>Common B</label>
<div>
<input type="text" formControlName="valueB">
<small>Description</small>
</div>
</div>
`
})


export class ComponentB implements OnInit{

@Input('group')
public myForm: FormGroup;

constructor(private _fb: FormBuilder) {
}

ngOnInit() {
this.myForm= this._fb.group({
valueB : ['', [Validators.required]]
});
}
}

我的问题是如何在不将输入控件移至主组件的情况下使用这两个子组件组成一个表单。例如 main.component.ts

@Component({
selector: 'main',
template: `
<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
<div>
<common-a [group]="formA"></common-a>
<common-b [group]="formB"></common-b>
<div>
<button>Register!</button>
</div>
</div>
</form>
`
})


export class Main implements OnInit{

@Input('group')
public myForm: FormGroup;

public formA : FormGroup;

public formB : FormGroup;

constructor(private _fb: FormBuilder) {
}

ngOnInit() {
this.myForm = this._fb.group({
//how can I compose these two sub forms here
//leaving the form control names agnostic to this component
});
}
}

这个想法背后的概念是构建许多复杂的表单,这些表单共享它们的一些构建 block 。

也就是说,我不想让我的 Main 组件知道 formControlNames [valueA1,valueA2,valueB] 但会自动插入它们并在顶级表单组上更新/验证。

任何想法或指出正确的方向都会有所帮助。

最佳答案

这可以通过将我们的顶级 FormGroup 传递给子组件并让子组件使用 formGroupName< 将自己添加到更高级别的 FormGroup 来实现 这样上层 FormGroup 就不需要对下层一无所知了:

ma​​in.component.ts

template: `<...>
<common-a [parentForm]="myForm"></common-a>
<...>

我们还将删除不再使用的 formA 和 formB 声明。

component-a.component.ts [formGroup] 是我们的父组,formGroupName 是我们识别和附加组件控件的方式作为一个团队在更大的整体中工作(他们将嵌套在父组中)。

@Component({<...>
template: `
<div [formGroup]="parentForm">
<div class="form-group">
<label>Common A[1]</label>
<div formGroupName="componentAForm">
<input type="text" formControlName="valueA1">
<small>Description 1</small>
</div>
<div class="form-group">
<label>Common A[2]</label>
<div formGroupName="componentAForm">
<input type="text" formControlName="valueA2">
<small>Description 2</small>
</div>
</div>`
})

export class ComponentA implements OnInit {
@Input() parentForm: FormGroup;
componentAForm: FormGroup;

constructor(private _fb: FormBuilder) {}

ngOnInit() {
this.componentAForm = this._fb.group({
valueA1: ['', Validators.required],
valueA2: ['', Validators.required]
});

this.parentForm.addControl("componentAForm", this.componentAForm);
}
}

这是一个 plunker (请注意,我在这里让组件 B 稍微动态一些只是为了看看它是否可以完成,但上面的实现同样适用于 B):http://plnkr.co/edit/fYP10D45pCqiCDPnZm0u?p=preview

关于forms - 以 angular2 模型驱动形式重用组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40891559/

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