相关疑难解决方法(0)

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

我对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 …
Run Code Online (Sandbox Code Playgroud)

forms angular2-forms mod angular

9
推荐指数
1
解决办法
1766
查看次数

Reactive Forms: How to add new FormGroup or FormArray into an existing FormGroup at a later point in time in Angular 7

In the other examples at StackOverflow there are many questions about using FormGroups in FormArrays. But my question is the opposite.

FormArrays have a push method, that makes many things possible. FormGroups have indeed an addControl method for adding simple FormControls. Afaik FormGroups do not have addFormArray or addFormGroup method. Therefore I need your help.

Following situation:

this.myForm = this.fb.group({
  id: this.fb.control([this.book.id]),
  // or short form `id: [this.book.id],`
});
Run Code Online (Sandbox Code Playgroud)

Adding a simple control at a later point in time is …

angular angular-reactive-forms formarray formgroups

5
推荐指数
1
解决办法
2548
查看次数