What is @ng-select/ng-select?
@ng-select/ng-select is a highly customizable Angular component for creating select dropdowns. It provides a wide range of features including single and multiple selection, custom templates, filtering, and more.
What are @ng-select/ng-select's main functionalities?
Single Selection
This feature allows users to select a single item from a dropdown list. The `items` input takes an array of objects, and `bindLabel` specifies the property to display in the dropdown.
<ng-select [items]="cities" bindLabel="name" placeholder="Select a city"></ng-select>
Multiple Selection
This feature enables users to select multiple items from the dropdown. Setting the `multiple` input to `true` allows for multiple selections.
<ng-select [items]="cities" bindLabel="name" [multiple]="true" placeholder="Select cities"></ng-select>
Custom Templates
Custom templates allow for more control over the appearance of the dropdown items. The `ng-option-tmp` directive can be used to define a custom template for each item.
<ng-select [items]="cities" bindLabel="name" placeholder="Select a city">
<ng-template ng-option-tmp let-item="item" let-index="index">
<div class="custom-option">
<span>{{item.name}}</span>
</div>
</ng-template>
</ng-select>
Filtering
The filtering feature allows users to search through the dropdown items. Setting the `searchable` input to `true` enables this functionality.
<ng-select [items]="cities" bindLabel="name" [searchable]="true" placeholder="Select a city"></ng-select>
Async Data
This feature supports loading data asynchronously. The `items` input can take an observable, and the dropdown will update as the data is loaded.
<ng-select [items]="cities$ | async" bindLabel="name" placeholder="Select a city"></ng-select>
Other packages similar to @ng-select/ng-select
ngx-select-dropdown
ngx-select-dropdown is another Angular package for creating dropdowns. It offers similar functionalities such as single and multiple selection, custom templates, and filtering. However, it may not be as feature-rich or customizable as @ng-select/ng-select.
ng-multiselect-dropdown
ng-multiselect-dropdown is focused on providing a multi-select dropdown component for Angular. It supports features like filtering and custom templates but is primarily designed for multiple selections, unlike @ng-select/ng-select which supports both single and multiple selections.
angular-ng-autocomplete
angular-ng-autocomplete is an Angular component for creating autocomplete dropdowns. It provides features like filtering and custom templates, similar to @ng-select/ng-select, but is more focused on autocomplete functionality rather than general dropdowns.
Angular native ui-select
See Demos or try in Plunker
Warning
Library is under active development and may not work as expected until stable 1.0.0 release.
Getting started
After installing the above dependencies, install ng-select
via:
npm install --save @ng-select/ng-select
Once installed you need to import our main module:
import {NgSelectModule} from '@ng-select/ng-select';
The only remaining part is to list the imported module in your application module.:
import {NgSelectModule} from '@ng-select/ng-select';
@NgModule({
declarations: [AppComponent, ...],
imports: [NgSelectModule, ...],
bootstrap: [AppComponent]
})
export class AppModule {
}
SystemJS
If you are using SystemJS, you should also adjust your configuration to point to the UMD bundle.
In your systemjs config file, map
needs to tell the System loader where to look for ng-select
:
map: {
'@ng-select/ng-select': 'node_modules/@ng-select/ng-select/bundles/ng-select.umd.js',
}
Roadmap
Usage
Input | Type | Default | Required | Description |
---|
[items] | Array | [] | yes | Items array |
labelKey | string | label | no | Bind option display text to object property. Default label |
valueKey | string | value | no | Bind selected option model value to property or whole object if used as bindValue="this" . Default value |
[clearable] | boolean | true | no | Set is allowed to clear selected value. Default true |
placeholder | string | null | no | Set placeholder text. Default null |
[typeahead] | Subject | null | no | Set custom filter. Default null |
Output | Description |
---|
(focus) | Fired on select focus |
(blur) | Fired on select blur |
(change) | Fired on selected value change |
(open) | Fired on select dropdown open |
(close) | Fired on select dropdown close |
Contributing
Contributions are welcome. You can start by looking at issues with label Help wanted https://github.com/ng-select/ng-select/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22 or creating new Issue with proposal or bug.
Clone and play
Perform the clone-to-launch steps with these terminal commands.
Run demo
git clone https://github.com/ng-select/ng-select
cd ng-select
npm install
npm run serve-demo
Testing
npm run test
or
npm run test-watch