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

2023-11-27

我对 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
        });
    }
}

这个想法背后的概念是构建许多共享一些构建块的复杂形式。

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

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


这可以通过传递我们的顶层来完成FormGroup到子组件并使子组件将其自身添加到更高级别FormGroup using formGroupName这样上层FormGroup基本上不需要了解较低级别的信息:

主要组件.ts

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

我们还将删除 formAL、表单声明,因为它们不再使用。

组件-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);
     }
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

以 angular2 模型驱动形式重用组件 的相关文章

随机推荐