npm install --save @ng-select/ng-select
import { NgSelectModule } from '@ng-select/ng-select';
...
@NgModule({ imports: [ ... NgSelectModule ], ... })
import { Component, OnInit } from '@angular/core';
interface City { id: number; name: string; }
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { cities: City[]; selectedCity: City;
ngOnInit() {
this.cities = [
{ id: 1, name: 'New York' },
{ id: 2, name: 'London' },
{ id: 3, name: 'Tokyo' },
// add more cities here
];
}
})
这样,就可以在 Angular 项目中使用 ng-select 组件了。