我正在从我的API中检索车辆数据的信息。然后验证字段。
我的ts文件如下:
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;
});
}
}
其中一个的HTML看起来像这样:
<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>
但是我的输入并不反映来自API的数据。如果您在后面查看代码,则该值在那里,但没有显示出来。
我做错了什么?
答案 0 :(得分:2)
收到回复后,您需要设置表格。
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] ]
});
});
或者您可以设置带有响应的表单值。
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
});
});
答案 1 :(得分:0)
就我而言,有必要在回调中手动运行更改检测。 这可以通过导入来实现
internal fun switchWifi(value: Boolean) {
val wifiManager = ApplicationProvider.getApplicationContext<YourApplicationClass>().getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.isWifiEnabled = value}
并运行
constructor(private changeDetectorRef: ChangeDetectorRef) {
}