Search code examples
angularangular-reactive-formsangular-forms

Angular Reactive forms - Value from Async call not updating form


I'm retrieving information for vehicle data from my API. And then validate the fields.

My ts file looks like this:

vehicleDetails = new CartellInfo();

ngOnInit() {
    this.claimForm = this.formBuilder.group({
        plateNumber: [ this.vehicleDetails.registration, [Validators.required] ],
        vehicleManufacturer: [ this.vehicleDetails.make, [Validators.required] ],
        vehicleModel: [ this.vehicleDetails.model, [Validators.required] ],
        vehicleRange: [ this.vehicleDetails.description, [Validators.required] ],
        chasisNumber: [ this.vehicleDetails.chassisNumber, [Validators.required] ],
        fuelType: [ this.vehicleDetails.fuelType, [Validators.required] ],
        dateFirstRegistered: [ this.vehicleDetails.firstRegistrationDate, [Validators.required] ],
        engineCapacity: [ this.vehicleDetails.engineCapacity, [Validators.required] ]
    });
}

findVehicle() {
    if (this.registration !== null) {

        this.cartellService.findVehicle(this.registration)
        .subscribe(
        res => {
            this.vehicleFound = true;
            this.vehicleDetails = res;
        });
    }
}

And the HTML for one of them looks like this:

<button class="btn btn-secondary" (click)="findVehicle()">FIND VEHICLE</button>

<input class="form-control"
    [ngClass]="{'mat-form-field-readonly': vehicleDetails.model || available }"
    [readonly]="vehicleDetails.model || available"
    name="vehicleModel"
    [formControl]="f.vehicleModel"
>
<mat-error *ngIf="submitted && f.vehicleModel.hasError('required')">
    Vehicle model is <strong>required</strong>
</mat-error>

But my input doesn't reflect data coming from the API. If you look code behind, the value is there, but it's not showing up.

What I'm doing wrong?


Solution

  • You need to set form after you receive the response.

    this.cartellService.findVehicle(this.registration)
      .subscribe(
        res => {
          this.vehicleFound = true;
          this.vehicleDetails = res;
          this.claimForm = this.formBuilder.group({
              plateNumber: [ this.vehicleDetails.registration, [Validators.required] ],
              vehicleManufacturer: [ this.vehicleDetails.make, [Validators.required] ],
              vehicleModel: [ this.vehicleDetails.model, [Validators.required] ],
              vehicleRange: [ this.vehicleDetails.description, [Validators.required] ],
              chasisNumber: [ this.vehicleDetails.chassisNumber, [Validators.required] ],
              fuelType: [ this.vehicleDetails.fuelType, [Validators.required] ],
              dateFirstRegistered: [ this.vehicleDetails.firstRegistrationDate, [Validators.required] ],
              engineCapacity: [ this.vehicleDetails.engineCapacity, [Validators.required] ]
          });
        });
    

    Or you can set the form value with the response.

    this.cartellService.findVehicle(this.registration)
      .subscribe(
        res => {
          this.vehicleFound = true;
          this.vehicleDetails = res;
          this.claimForm.setValue({
              plateNumber: this.vehicleDetails.registration,
              vehicleManufacturer: this.vehicleDetails.make,
              vehicleModel: this.vehicleDetails.model,
              vehicleRange: this.vehicleDetails.description,
              chasisNumber: this.vehicleDetails.chassisNumber,
              fuelType: this.vehicleDetails.fuelType,
              dateFirstRegistered: this.vehicleDetails.firstRegistrationDate,
              engineCapacity: this.vehicleDetails.engineCapacity
          });
        });