作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
每当用户开始搜索某些内容时,我都会遇到一个场景,该内容将调用 HTTP 服务并获取响应并将其显示在下拉列表中。
我有下面的代码,可以很好地使用上述方法。但是,在我们单击下拉列表中列出的任何选项后,ngmodelchange
方法会再次调用该方法来再次获取服务。这不应该发生。
我哪里失踪了?
<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>
完整代码在 stackblitz
最佳答案
尝试将 ngModelChange
事件更改为 keypress
事件
<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"
(keypress) = "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>
ngModelChange gets called if there is a change in the input value but the keypress event gets called once you type any value
关于javascript - 在 Angular Material 中选择 mat-option 时调用 ngmodelchange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54822491/
我是一名优秀的程序员,十分优秀!