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

ste*_*nos 9 forms angular2-forms mod angular

我对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]],
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一个组件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]]
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何使用这两个子组件组成表单而不将输入控制移动到主组件.例如amain.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
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

也就是说,我不希望我的Main组件知道的名字formControlNames[ valueA1,valueA2,valueB],但自动地将它们插入和更新/验证在顶层表单组.

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

sil*_*sod 10

这可以通过将我们的顶层传递FormGroup给子组件并让子组件FormGroup使用formGroupName这种方式将其自身添加到更高级别来实现,上层FormGroup需要基本上不了解较低级别:

main.component.ts

template: `<...>
    <common-a [parentForm]="myForm"></common-a>
    <...>
Run Code Online (Sandbox Code Playgroud)

我们还将删除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);
     }
}
Run Code Online (Sandbox Code Playgroud)

这里有一个plunker (注意我在这里使组件B更加动态,看看是否可以完成,但上面的实现同样适用于B):http://plnkr.co/edit/fYP10D45pCqiCDPnZm0u?p =预习