Angular5-如何在m​​at-autocomplete中检测空白或已删除字段?

Aak*_*rma 4 javascript angular-material angular5 mat-autocomplete

这是我的html,其中包含材料自动完成功能。

<ng-container *ngSwitchCase="'autocomplete'">
    <div [ngClass]="(filter.cssClass || 'col-md-4') + ' mt-2 pt-1'">
        <div class="filter-key-label">
            {{filter.columnTitle}}
        </div>
        <div class="filter-form-control d-flex flex-wrap justify-content-left">
        <mat-form-field class="full-width">
            <input type="text" matInput [formControl]="myControl" [matAutocomplete]="auto" ng-model="blah">
            <mat-autocomplete #auto="matAutocomplete">
                <mat-option *ngFor="let option of getOptionsTypeAhead(filter.key) | async" [value]="option" (onSelectionChange)="typeaheadChange($event, filter.key)">
                {{ option }}
                </mat-option>
            </mat-autocomplete>
            </mat-form-field>
        </div>
    </div>
</ng-container>
Run Code Online (Sandbox Code Playgroud)

目前,我只能使用来检测选择更改onSelectionChange但是无法检测到该字段最初是否为空,或者在选择了某个项目然后将其删除后是否为空,因此我什么也不会选择,也不会将焦点移到输入字段之外。

如何检测到?

我尝试过onkeypressng-change再加上ng-model(不确定如何使用),change但是没有任何效果。库是否已经提供了某些规定,或者我们检测到更改并输入了字段值?

Aak*_*rma 5

解决了!一直change都是。

最好的部分是它的行为不一样onkeypress,仅在发生blur事件时才触发。

我这样改变了我<input>

<input type="text" matInput [formControl]="myControl" [matAutocomplete]="auto" (change)="handleEmptyInput($event, filter.key)">
Run Code Online (Sandbox Code Playgroud)

控制器看起来像这样:

handleEmptyInput(event: any, key: string){
  if(event.target.value === '') {
    delete this.filtersApplied[key];
  }
}
Run Code Online (Sandbox Code Playgroud)

就像我想要捕获空字段或空白值的实例一样:)