Multiple select Angular
Create A New Angular ProjectUsing Ng CLI tool we will create a new Angular project by running below command in terminal:
$ ng new ng-select-demoInstall & Configure Ng-Select
After creating an Angular project next we will install ng-select page.
Step 1) Run the following command in terminal to install ng-select:
$ npm install --save @ng-select/ng-selectStep 2) To use ng-select component, we need to import NgSelectModule & FormsModule in app.module.ts file:
import { BrowserModule } from '@angular/platform-browser';We already have three themes available to select from. Import any of the following in the style.css file:
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NgSelectModule } from '@ng-select/ng-select';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
NgSelectModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
@import "~@ng-select/ng-select/themes/default.theme.css";At file AppComponent.component.html
<ng-select [items]="cities2"At AppComponent.component.ts
bindLabel="name"
bindValue="id"
[multiple]="true"
placeholder="Select cities"
[(ngModel)]="selectedCityIds">
</ng-select>
cities2 = [selectedCityIds: string[];
{id: 1, name: 'Vilnius'},
{id: 2, name: 'Kaunas'},
{id: 3, name: 'Pavilnys', disabled: true},
{id: 4, name: 'Pabradė'},
{id: 5, name: 'Klaipėda'}
];
Method constructor
constructor() {
this.create10kCities();
}
private create10kCities() {
this.cities4 = Array.from({length: 10000}, (value, key) => key)
.map(val => ({
id: val,
name: `city ${val}`
}));
}