angular - 如何转换为可编辑的选择框?

标签 angular bootstrap-4 angular-ui ui-select2

我有一个带有 Bootstrap 的 Angular 5 项目。在我的一个组件 html 中,我有一个选择框。当我点击它时,会显示一个下拉项目列表,我可以选择一个。但是我希望用户输入一些字符串,以便可以缩小下拉项目的范围。这是我当前的代码片段,知道如何将选择框转换为可编辑的。

   <select formControlName="contactList" [compareWith]="compareResource">
      <option value="" disabled>{{ 'PLACEHOLDERS.CONTACT_LIST' | translate }}</option>
      <option *ngFor="let contactList of contactLists" [ngValue]="contactList">{{ contactList.name }}</option>
    </select>

我想将现有的选择框代码转换为可编辑的选择框。请分享您对此的想法。我对 UI 编程非常陌生。谢谢你

最佳答案

这可以通过使用 ng-select 来实现

安装 ng-select

npm install --save @ng-select/ng-select

app.module.ts

将其导入模块
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { NgSelectModule } from '@ng-select/ng-select';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule,
    CommonModule,
    NgSelectModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

样式.css

导入 ng-select 默认样式
@import "~@ng-select/ng-select/themes/default.theme.css";

app.component.html

通过将 nameList 绑定(bind)为下拉和搜索功能的项目数组来创建 ng-select 下拉列表
<div style="text-align:center">
  Editable Dropdown
</div>

<div style="width:300px; height:200px">
  <ng-select class="autocomplete" dropdownPosition="bottom" [searchFn]="searchName" [items]="nameList"
    [(ngModel)]="selectedName" [dropdownPosition]="'auto'">
    <ng-template ng-header-tmp>
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-6">Username</div>
        </div>
      </div>
    </ng-template>
    <ng-template ng-label-tmp let-item="item">
      <span>{{item}}</span>
    </ng-template>
    <ng-template ng-option-tmp let-item="item" let-index="index">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-4">{{item}}</div>
        </div>
      </div>
    </ng-template>
  </ng-select>
</div>

app.component.ts

初始化 nameList 并定义搜索函数。在这里,我将返回包含从下拉列表中输入的值的名称
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  selectedName: string;
  nameList: string[];

  constructor() { }

  ngOnInit() {
    this.nameList = this.getNameList();
  }

  getNameList(): string[] {
    return [
      'Adam',
      'Alex',
      'Bob',
      'Bennt',
      'Cathrina',
      'Dug',
      'Suzzy',
      'Amy',
      'Milan'
    ];
  }

  searchName(filter: string, item: string) {
    filter = filter.toLocaleLowerCase();
    return (item.toLocaleLowerCase().indexOf(filter) > -1);
  }
}

关于angular - 如何转换为可编辑的选择框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55519382/

相关文章:

加载时的 angularjs 值不正确

javascript - Angular 2多重倒计时管道

angular - mat-cell Angular 8 中的 if else

javascript - Angular 2 - 使用 Angular-route 3.3.0 覆盖路线

css - Bootstrap 4 更改工具提示箭头颜色

CSS: div 搜索元素右对齐 (Bootstrap)

angularjs - Angular UI-Grid(新的)——基于行的动态/自动调整高度

javascript - 如何正确使用javascript promises? ( ionic 和 Cordova 相关)

forms - 内嵌表单输入和提交按钮在屏幕宽度 < 576 px 时未对齐

AngularJS - ng-repeat 一次显示一项