订阅方法后未设置变量

时间:2019-05-16 13:05:00

标签: angular angular-httpclient

在失去对输入的关注之后,我在输入上发生了事件,我试图从API获取数据,数据是正确的,但是在再次关注输入之后,首先没有进行任何设置。

我的服务

public getContractorByNIP(NIP: string) {
    let httpParam = new HttpParams().set('NIP', NIP);
    return this.httpClient.get<GpContractor>('http://localhost:8080/InvoiceAPI/invoice/contractor', {params: httpParam});
  }

我的组件

onFocusLost() {
    if (this.contractorNIP.valid){
      this.getContractorByNIP(this.contractorNIP.value.toString());
    }
  }

  getContractorByNIP(NIP: string) {
      this.contractorService.getContractorByNIP(NIP)
        .subscribe((data) => this.contractor = data);
  }

我的html

<div class="col-md-6">
 <mat-form-field>
  <input matInput placeholder="NIP kontrahenta" formControlName="gpContractorNIP" class="matIn" (focusout)="onFocusLost()" minlength="10" maxlength="10" pattern="[0-9]{10}$">
 </mat-form-field>
</div>

我希望在失去焦点后的第一时间设置数据。

1 个答案:

答案 0 :(得分:0)

this.contractorNIP.value.toString()应该是this.gpContractorNIP.value.toString()

还使用了formControl,因此需要通过在this.gpContractorNIP上订阅valueOnChanges来获取模型的值。