如何将 Angular FormArray 字符串化以便将其发送到服务器

How to Stringify Angular FormArray in order to send it to the server

在 Angular 项目中,我有一个包含 FormControl 的 FormArray,我不知道如何将这个 FormArray 字符串化以便将它发送到服务器。这是代码、提交方法和我创建 Observable 的服务。请帮忙

onSubmit(){
    let formControls = new FormArray([]);
    formControls = <FormArray>this.reviewForm.get('controlArray');
    this.formService.createForm(formControls)
      .subscribe(
        data => console.log(data),
        error => console.error(error)
    );
    this.reviewForm.reset();
    // console.log(formControls);        
  }

@Injectable()
export class FormService {
    constructor(private http: Http) {}

    createForm(formControls: FormArray) {
        const body = JSON.stringify(formControls); //this gives error
        const headers = new Headers({'Content-Type': 'application/json'});
        return this.http.post('http://localhost:3000/api/form', body, {headers: headers})
            .map((response: Response) => response.json())
            .catch((error: Response) => Observable.throw(error.json()));
    }

}

您可以使用getRawValue()方法。

formControls.getRawValue();

只需获取值并将其字符串化:

JSON.stringify(this.reviewForm.value.controlArray)