简体   繁体   English

Angular 5使用ng-select作为自动完成字段

[英]Angular 5 use ng-select as an autocomplete field

select from ... in my application. 从我的应用程序中选择.... Is it possible to use it as an autocomplete field, the select value is not required. 是否可以将其用作自动完成字段,不需要选择值。 I want to use ng-select because it uses a virtual scroll and the mat-autocomplete from angular material doesn't. 我想使用ng-select,因为它使用虚拟滚动而角度材质的mat-autocomplete则不然。 And with a lot of values the mat-autocomplete becomes slow. 并且有很多值,mat-autocomplete变得很慢。

My question: Is it possible to use the ng-select just as an autocomplete function. 我的问题:是否可以将ng-select用作自动完成功能。 In other words: ng-select without the required select option. 换句话说:ng-select没有必需的选择选项。 If I click away from the ng-select field, the value will be empty if nothing selected. 如果我点击ng-select字段,如果未选择任何内容,则该值将为空。 The value has to stay.. 价值必须保持..

<label>Your first ng-select</label>
      <ng-select class="custom" [items]="cities"
                bindLabel="name"
                placeholder="Select city"
                [typeahead]="typeahead"
                [(ngModel)]="selectedCity">
      </ng-select>

I believe you are using 我相信你在用

https://www.npmjs.com/package/@ng-select/ng-select#api https://www.npmjs.com/package/@ng-select/ng-select#api

it is not recommended to use it that way, how ever there is a work around. 不建议以这种方式使用它,如何解决问题。

Add the following listeners 添加以下侦听器

(open)= OnOpen(), (search) = OnSearch(), (blur) = OnBlue() (open)= OnOpen(),(search)= OnSearch(),(blur)= OnBlue()

<ng-select [items]="cities" #select1
                   bindLabel="name"
                   (open)= OnOpen()
                   (search) = OnSearch()
                   (blur) = OnBlue()
                   dropdownPosition="hidden"
                   [(ngModel)]="selectedCity">
        </ng-select>

Add define these two variables 添加定义这两个变量

isbeingSearched: boolean = false;
@ViewChild('select1') select1Comp: NgSelectComponent;

//Code to handle events //处理事件的代码

OnOpen(){
  console.log("OnOpen");
  if(!this.isbeingSearched)
  {
    this.select1Comp.close()      
  }

}

OnSearch(){
  this.isbeingSearched = true;
  console.log("OnSearch");
  this.select1Comp.open()
}

OnBlue(){
  console.log("OnBlue");
  this.isbeingSearched = false;
  this.select1Comp.close()
}

Working example 工作实例

https://stackblitz.com/edit/ng-select-lw4dfh https://stackblitz.com/edit/ng-select-lw4dfh

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

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