无法访问 Angular 中 formArray 的值

Not able to access value of formArray in Angular

我无法在我的 Angular 项目

中使用 Reactive 表单访问 formarray 值

我的回复是这样的:

{出现一次:"InitialValue" formArray: 数组[0]}

在表单数组中输入值并提交表单后我仍然得到数组 0

我正在分享我的代码,它存在于 slack-blitz link https://stackblitz.com/edit/angular-x6mwfj?file=src%2Fapp%2Fapp.component.ts

代码:

TS :

 inputArray: any = [];
myForm: FormGroup;
 constructor(private fb: FormBuilder) {}
    ngOnInit(): void {
        let newForm = this.fb.group({
            appearsOnce: ['InitialValue', [Validators.required, Validators.maxLength(25)]],
            formArray: this.fb.array([])
        });

        const arrayControl = <FormArray>newForm.controls['formArray'];
        this.inputArray.forEach(item => {
            let newGroup = this.fb.group({
                propertyOne: [''],
                propertyTwo: ['']
            });
            arrayControl.push(newGroup);
        });

        this.myForm = newForm;
    }
    addInput(): void {

        const arrayControl = <FormArray>this.myForm.controls['formArray'];
        let newGroup = this.fb.group({

           propertyOne: ['InitialValue'],
                propertyTwo: ['InitialValue']

        });
        arrayControl.push(newGroup);
    }
    delInput(index: number): void {
        const arrayControl = <FormArray>this.myForm.controls['formArray'];
        arrayControl.removeAt(index);
    }
    onSubmit(): void {
        console.log(this.myForm.value);

    }

html :

<form [formGroup]="myForm" (ngSubmit)="onSubmit()" novalidate>
    <label>Appears Once:</label>
    <input type="text" formControlName="appearsOnce" />






    <div formArrayName="formArray">
        <div *ngFor="let control of myForm.controls['formArray'].controls; let i = index">
            <button type="button" (click)="delInput(i)">Delete</button>
            <!-- <my-form-sub-component [myForm]="myForm.controls.formArray.controls[i]"></my-form-sub-component> -->
            <div [formGroup]="newGroup">
    <label>Property One: </label>
    <input type="text" formControlName="propertyOne"/>

    <label >Property Two: </label>
    <input type="number" formControlName="propertyTwo"/>
</div>
        </div>
    </div>
    <button type="button" (click)="addInput()">Add + </button>
    <button type="submit" class='btn' [disabled]="!myForm.valid">Submit</button>
</form>

FormArray 是索引 based.So 你必须像这样将索引传递给 formGroupName 指令

component.html

<form [formGroup]="myForm" (ngSubmit)="onSubmit()" novalidate>
    <label>Appears Once:</label>
    <input type="text" formControlName="appearsOnce" />
    <div formArrayName="formArray">
        <div *ngFor="let control of myForm.get('formArray').controls; let i = index">
            <button type="button" (click)="delInput(i)">Delete</button>
            <div [formGroupName]="i">
                <label>Property One: </label>
                <input type="text" formControlName="propertyOne"/>

                <label >Property Two: </label>
                <input type="number" formControlName="propertyTwo"/>
</div>
            </div>
        </div>
        <button type="button" (click)="addInput()">Add + </button>
        <button type="submit" class='btn' [disabled]="!myForm.valid">Submit</button>
</form>

Forked Example