简体   繁体   English

ngmodelchange在角度6材质中两次调用

[英]ngmodelchange called twice in angular 6 material

I have a scenario whenever the user is start searching something which will call the http service and get the response and show it in the dropdown. 每当用户开始搜索将调用http服务并获取响应并将其显示在下拉列表中的内容时,我都会遇到这种情况。

I have the below code which is working fine with the above approach. 我有下面的代码,可以与上述方法很好地工作。 But, after we click any option listed from the dropdown, ngmodelchange method again calling which fetch the service again. 但是,在单击下拉列表中列出的任何选项后, ngmodelchange方法将再次调用来再次获取服务。 It should not happen. 它不应该发生。

Where am I missing? 我在哪里想念?

<mat-form-field class="half-width">
            <input matInput aria-label="State" name="state" placeholder="Search by number (1 or 2 or 3)" [matAutocomplete]="auto" [(ngModel)]="currentState"
              (ngModelChange) = "filterInstrument(currentState)">
            <mat-autocomplete #auto="matAutocomplete" [displayWith]="state">
                <mat-option *ngFor="let state of insDetails" [value]="state.id">
                  <span>{{state.id}}</span> |
                  <span>{{state.userId}}</span> |
                  <span>{{state.title}}</span>                 
                </mat-option>
            </mat-autocomplete>
          </mat-form-field>

Also is there any possibility to show the selected value (pipe separated) as like shown in the dropdown? 也有可能显示下拉列表中所示的选定值(以竖线分隔)吗? Currently id property alone is displayed in the selected value. 在所选值中仅显示当前id属性。

Complete Code in stackblitz Stackblitz中的完整代码

Any help? 有什么帮助吗?

To show the current selected option in input add click to mat-option: 要在输入中显示当前选定的选项,请单击添加到mat-option:

<mat-option *ngFor="let state of insDetails" [value]="state.id" (click) ="valueGet(state)">
     <span >{{state.id}}</span> |
     <span>{{state.userId}}</span> |
     <span>{{state.title}}</span>                 
</mat-option>

In select-multiple-example.ts: 在select-multiple-example.ts中:

valueGet(state) {
   this.currentState = `${state.id} ${state.title} ${state.userId}`;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM