Serhio g. Lazin
Serhio g. Lazin

Reputation: 9642

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'. But controls must be declared.

form.value object

{
  "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> 

Upvotes: 1

Views: 2706

Answers (1)

Rub&#233;n Soler
Rub&#233;n Soler

Reputation: 1187

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

}

Upvotes: 2

Related Questions