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?
kilometers: [ '', [Validators.required] ]empty values to kilometers, then how will you get the value in form field??tapto construct theFormGroupafter the API call returns.