Angular 2 删除或添加数据到 form.value 对象

Angular 2 remove or add data to form.value object

我需要在提交前从 form.value 对象中删除 "toys",并将新数据添加到 'price'。但是必须声明控件。

form.value对象

{
  "red": 1,
  "green": 3,
  "black": "120",
  "blue": 3,
  "toys": [
    {
      "bear": 0,
      "soldier": 0,
      "car": 0
    }
  ],
  "price": [
    {
      "default": 123,
      "pre": 3,
      "after": 2
    },
    {
      "default": 3,
      "pre": 0,
      "after": 0
    }
  ]
}

ts

 initForm() {
        this.form = this._fb.group({
        red: 0,
        green: 0,
        black: '',
        blue: 0,
        toys: this._fb.array([this.inittoys()]),
        price: this._fb.array([this.initprice()]),

   });

html

 <div class="form-group">
   <label for="black">Max travel time</label>
    <select class="form-control" id="black" formControlName="black">
       <option *ngFor="let t of colors; let i=index" [ngValue]="i">{{t}}</option>
    </select>
</div> 

您可以在发送表单之前修改函数中的值,如下所示:

<form [formGroup]="yourForm" (ngSubmit)="yourSendFunction(yourForm.value)">

.....

在组件中:

yourSendFunction(values) {

  delete values.toys;

  values.price.push({
             // Here anything you wants to addd
              })

  // Next send the values

}