类型“FormArray”角度 6 上不存在属性“splice”

Vai*_*wad 1 angular angular-reactive-forms

如何将 formArray 中的 formgroup 推送到第 0 个索引处。它应该被 formArray 中的第 0 个元素替换。

Sid*_*era 5

按照 nicraft 的建议,insert使用FormArray. 您可能必须相应地管理模板。

在这里,尝试一下:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, FormControl } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  form: FormGroup;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.form = this.fb.group({
      array: this.fb.array([])
    });
  }

  addItem() {
    const formArray  = this.array;
    formArray.insert(0, this.fb.control(formArray.length + 1));
  }

  get array() {
    return (<FormArray>this.form.get('array'));
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:这里我只是FormControl向 中添加一个新值array FormArray,并将 的FormArray当前长度 + 1 设置为它的值。


这是一个StackBlitz 工作示例供您参考。