简体   繁体   English

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

[英]Angular 2 remove or add data to form.value object

I need to remove "toys" from form.value object before submit, and add new data to 'price'. 我需要在提交之前从form.value对象中删除“玩具”,并将新数据添加到“价格”中。 But controls must be declared. 但是必须声明控件。

form.value object 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 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 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> 

You can modify the values in the function before you send the form like this: 您可以在发送表单之前修改函数中的值,如下所示:

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

.... ....

in the component: 在组件中:

yourSendFunction(values) {

  delete values.toys;

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

  // Next send the values

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM