This can be achieved by using ng-select
Install ng-select
npm install --save @ng-select/ng-select
app.module.ts
Import it to the module
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 { }
style.css
import the ng-select default styling
@import "~@ng-select/ng-select/themes/default.theme.css";
app.component.html
Create the ng-select dropdown by binding nameList as the items array for the dropdown and search function
<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
Initialize the nameList and define the search function. Here I will return names which contain the value entered from the dropdown
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);
}
}