加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:58:36 所属栏目:安全 来源:网络整理
导读:我对angular2很新,过去几天我一直在尝试使用模型驱动的表单创建可重用的表单组件 所以我们假设我们有一个组件componentA.component.ts @Component({ selector: 'common-a',template: ` div [formGroup]="_metadataIdentifier" div class="form-group" labelC
我对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 : ['',});
    }
}

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

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

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

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

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

main.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读