TheScripterX
TheScripterX

Reputation: 268

Bind Array Data with the Reactive Form

While learning Angular, I am stuck on a problem.

I have a form that uses the reactive method.

I have an array "models" with the "price" of each "model"

I would like that when I choose the "model", it should give me its "price", and when I validate the form, I receive the chosen model and its price in a console.log (this.form.value)

This is my HTML:

 <form [formGroup]="factureForm" (ngSubmit)="onSubmitForm()">
  <select formControlName="model">
    <option *ngFor="let model of models">{{ model.model }}</option>
  </select>
  <select formControlName="price">
    <option *ngFor="let model of models">{{ model.price }}</option>
  </select>
  <button type="submit">Submit</button>
</form>

This is my TS :

import { Component, OnInit } from "@angular/core";
import { FormsModule, FormGroup, FormBuilder } from "@angular/forms";

@Component({
  selector: "app-relational-data",
  templateUrl: "./relational-data.component.html",
  styleUrls: ["./relational-data.component.css"],
})
export class RelationalDataComponent implements OnInit {
  factureForm: FormGroup;
  models = [
    {
      model: "Model 1",
      price: 20,
    },
    {
      model: "Model 2",
      price: 50,
    },
  ];

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    this.initFactureForm();
  }

  initFactureForm() {
    this.factureForm = this.formBuilder.group({
      model: [""],
      price: [""],
    });
  }

  onSubmitForm() {
    const newFacture = this.factureForm.value;
    console.log(newFacture);
  }
}

I'm lost. Thank you in advance :)

Upvotes: 0

Views: 1271

Answers (2)

Nayak
Nayak

Reputation: 772

As you require to change the price on the change of model, you might need to set the price when the model changes. And you won't need a dropdown for the price too, as it's dependant on the model.

<form [formGroup]="factureForm" (ngSubmit)="onSubmitForm()">
  <select formControlName="model">
    <option value=''>Select</option>
    <option *ngFor="let model of models">{{model.model}}</option>
  </select>
  <input type="text" formControlName="price">
  <button type="submit">Submit</button>
</form>

initFactureForm() {
  this.factureForm = this.formBuilder.group({
    model: [""],
    price: [""],
  });

  // Look for changes to the model form-control
  this.factureForm.get('model').valueChanges.subscribe(newValue => {
    // newValue will be holding the 'model' attribute of the selected model
    // Searching the models array for the item with the selected model name
    const selectedModel = this.models.find(item => item.model === newValue);
    // If the item is found in the array,
    // then set the price of the model item to the price form-control.
    // If not found, set price to ''
    if (selectedModel) {
      this.factureForm.get('price').setValue(selectedModel.price);
    } else {
      this.factureForm.get('price').setValue('');
    }
  });
}

Upvotes: 2

Soham
Soham

Reputation: 725

I think [ngValue] is missing.

<form [formGroup]="factureForm" (ngSubmit)="onSubmitForm()">
  <select formControlName="model">
    <option *ngFor="let model of models" [ngValue]="model.model">{{ model.model }}</option>
  </select>
  <select formControlName="price">
    <option *ngFor="let model of models" [ngValue]="model.price">{{ model.price }}</option>
  </select>
  <button type="submit">Submit</button>
</form>

Upvotes: 0

Related Questions