Ionic angular-ng-autocomplete在外部点击时不会关闭下拉列表

djmepvbi  于 2023-02-01  发布在  Ionic
关注(0)|答案(1)|浏览(178)

我尝试在我的离子5应用程序(Angular 12)中使用“angular-ng-autocomplete”(https://github.com/gmerabishvili/angular-ng-autocomplete)插件,但由于某种原因,当我点击下拉列表框外时,它没有关闭下拉列表。
我也试过手动操作,但还是不行。下面是我的手动代码
超文本标记语言

<div class="ng-autocomplete">
  <ng-autocomplete #auto
    [data]="items"
    [searchKeyword]="keyword"
    placeholder="Name"
    (selected)='selectEvent($event)'
    debounceTime='300'
    (inputChanged)='onChangeSearch($event)'
    (inputFocused)='onFocusChanged($event)'
    [initialValue]='defaultValue'
    [itemTemplate]="itemTemplate"
    [notFoundTemplate]="notFoundTemplate">
  </ng-autocomplete>

  <ng-template #itemTemplate let-item>
    <a [innerHTML]="item.name"></a>
  </ng-template>
   
  <ng-template #notFoundTemplate let-notFound>
    <div [innerHTML]="notFound"></div>
  </ng-template> 
</div>

分量

@ViewChild('auto') auto;

  @HostListener('document:click', ['$event'])
  clickOut(event) {
      if (!event.target.attributes['autocomplete'] || event.target.attributes['autocomplete'].name !== 'autocomplete') 
      {
        this.auto.close();
      }
   }

我试过调试它,但是看不出问题出在哪里。这里有一个视频https://streamable.com/b3aowi
任何建议都将不胜感激

laximzn5

laximzn51#

<ng-autocomplete #auto
  [data]="items"
  [searchKeyword]="keyword"
  placeholder="Name"
  (selected)='selectEvent($event)'
  debounceTime='300'
  (inputChanged)='onChangeSearch($event)'
  (inputFocused)='onFocusChanged($event)'
  [initialValue]='defaultValue'
  [itemTemplate]="itemTemplate"
  [notFoundTemplate]="notFoundTemplate"
  (click)="$event.preventDefault()"
  >

</ng-autocomplete>

我尝试使用preventDefault()**(单击)="$event.preventDefault()"**进行此事件绑定
它对我来说很好。希望这个答案会有所帮助

相关问题