在角度6中填写密码时如何从http调用的响应中获取城市和州的值

时间:2019-03-22 00:51:26

标签: events event-handling angular6 angular-httpclient

通过输入密码来填充城市和州的值,我编写了此代码,该代码有时但并非总是如此。 问题-每次输入一个数字都会调用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>

0 个答案:

没有答案