通过输入密码来填充城市和州的值,我编写了此代码,该代码有时但并非总是如此。 问题-每次输入一个数字都会调用http服务,因此,直到我输入完整的6位数字,它都会调用6次http,而对于第5位数字,它将填充州和城市的值。 尽管字段已填充
,但仍给出错误消息,告知返回的响应是未定义的component.ts
ngOnInit(): void {
this.policyService.getPolicyInfo().subscribe(response => {
this.vehicleDetailsForm.patchValue( {'field':null} )
})
}
onSubmitPin() {
let formValuePin: any = {
pincode: this.proposalDetailsForm.value.p_pin,
}
console.log(this.proposalDetailsForm.value.p_pin)
this.policyService.getPinCode(formValuePin)
.pipe()
.subscribe(vehpin => {
this.displayVehPin(vehpin),
console.log('vehpin: ', vehpin); //
(error: any) => this.errorMessage = <any>error
}
)
}
displayVehPin(vehpin): void {
this.vehidv = vehpin;
console.log('vehpin: ', vehpin); //
if (this.proposalDetailsForm) {
console.log("you almost der...")
}
// the data on the form
this.proposalDetailsForm.patchValue({
p_state: vehpin.data.state_name,
p_city: vehpin.data.city_name,
});
console.log(vehpin)
}
HTML代码
<div class="col-lg-4 m-form__group-sub">
<mat-form-field class="example-full-width" appearance="outline">
<mat-label>Pin Code</mat-label>
<input matInput placeholder="" formControlName="p_pin" (onChanges)="onSubmitPin()">
</mat-form-field>
<label *ngIf="proposalDetailsForm.get('p_pin').errors?.required" class="invalid-feedback">
This Field is required.
</label>
<label *ngIf="proposalDetailsForm.controls.p_pin.errors?.pattern" class="invalid-feedback">
Should be a valid 6 digit number.
</label>
</div>