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 ng-select - Lightweight all in one UI Select, Multiselect and Autocomplete
See Demos or try in Stackblitz or Plunker
Table of contents
Features
Warning
Library is under active development and may have API breaking changes for subsequent major versions after 1.0.0.
Getting started
Step 1: Install ng-select
:
NPM
npm install --save @ng-select/ng-select
YARN
yarn add @ng-select/ng-select
Step 2: Import the NgSelectModule and angular FormsModule module:
import { NgSelectModule } from '@ng-select/ng-select';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent],
imports: [NgSelectModule, FormsModule],
bootstrap: [AppComponent]
})
export class AppModule {}
Step 3: Include a theme:
To allow customization and theming, ng-select
bundle includes only generic styles that are necessary for correct layout and positioning. To get full look of the control, include one of the themes in your application. If you're using the Angular CLI, you can add this to your styles.scss
or include it in angular-cli.json
.
@import "~@ng-select/ng-select/themes/default.theme.css";
@import "~@ng-select/ng-select/themes/material.theme.css";
Step 4 (Optional): Configuration
You can also set global configuration and localization messages by providing custom NG_SELECT_DEFAULT_CONFIG
providers: [
{
provide: NG_SELECT_DEFAULT_CONFIG,
useValue: {
notFoundText: 'Custom not found'
}
}
]
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',
}
API
Inputs
Input | Type | Default | Required | Description |
---|
[addTag] | boolean | ((term: string) => any | Promise<any>) | false | no | Allows to create custom options. |
addTagText | string | Add item | no | Set custom text when using tagging |
appendTo | string | null | no | Append drodown to body or any other element using css selector |
bindValue | string | - | no | Object property to use for selected model. By default binds to whole object. |
bindLabel | string | label | no | Object property to use for label. Default label |
closeOnSelect | boolean | true | no | Whether to close the menu when a value is selected |
clearAllText | string | Clear all | no | Set custom text for clear all icon title |
[clearable] | boolean | true | no | Allow to clear selected value. Default true |
[compareWith] | (a: any, b: any) => boolean | (a, b) => a === b | no | A function to compare the option values with the selected values |
dropdownPosition | bottom | top | auto | auto | no | Set the dropdown position on open |
[groupBy] | string | Function | null | no | Allow to group items by key or function expression |
[selectableGroup] | boolean | false | no | Allow to select group when groupBy is used |
[items] | Array<any> | [] | yes | Items array |
loading | boolean | - | no | You can set the loading state from the outside (e.g. async items loading) |
loadingText | string | Loading... | no | Set custom text when for loading items |
[markFirst] | boolean | true | no | Marks first item as focused when opening/filtering. Default true |
maxSelectedItems | number | none | no | When multiple = true, allows to set a limit number of selection. |
hideSelected | boolean | false | no | Allows to hide selected items. |
multiple | boolean | false | no | Allows to select multiple items. |
notFoundText | string | No items found | no | Set custom text when filter returns empty result |
placeholder | string | - | no | Placeholder text. |
[searchable] | boolean | true | no | Allow to search for value. Default true |
[searchFn] | (term: string, item: any) => boolean | null | no | Allow to search by custom search function expression |
[selectOnTab] | boolean | false | no | Select marked dropdown item using tab. Default false |
[typeahead] | Subject | - | no | Custom autocomplete or filter. |
typeToSearchText | string | Type to search | no | Set custom text when using Typeahead |
[virtualScroll] | boolean | false | no | Enable virtual scroll for better performance when rendering a lot of data |
Outputs
Output | Description |
---|
(add) | Fired when item is selected |
(blur) | Fired on select blur |
(change) | Fired on selected value change |
(close) | Fired on select dropdown close |
(clear) | Fired on clear icon click |
(focus) | Fired on select focus |
(open) | Fired on select dropdown open |
(remove) | Fired when item is removed |
(scrollToEnd) | Fired when scrolled to the end of items. Can be used for loading more items in chunks. |
Methods
Name | Description |
---|
open | Opens the select dropdown panel |
close | Closes the select dropdown panel |
focus | Focuses the select element |
Other
Name | Type | Description |
---|
[ngOptionHighlight] | directive | Highlights search term in option. Accepts search term. Should be used on option element. |
Change Detection
Ng-select component implements OnPush
change detection which means the dirty checking checks for immutable
data types. That means if you do object mutations like:
this.items.push({id: 1, name: 'New item'})
Component will not detect a change. Instead you need to do:
this.items.push({id: 1, name: 'New item'})
this.items = [...this.items];
This will cause the component to detect the change and update. Some might have concerns that
this is a pricey operation, however, it is much more performant than running ngDoCheck
and
constantly diffing the array.
Custom styles
If you are not happy with default styles you can easily override them with increased selector specificity or creating your own theme. E.g.
<ng-select class="custom"></ng-select>
.ng-select.custom {
border:0px;
min-height: 0px;
border-radius: 0;
}
.ng-select.custom .ng-select-container {
min-height: 0px;
border-radius: 0;
}
Validation state
By default when you use reactive forms validators or template driven forms validators css class ng-invalid
will be applied on ng-select. You can show errors state by adding custom css style
ng-select.ng-invalid.ng-touched .ng-select-container {
border-color: #dc3545;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 3px #fde6e8;
}
Contributing
Contributions are welcome. You can start by looking at issues with label Help wanted or creating new Issue with proposal or bug report.
Note that we are using https://conventionalcommits.org/ commits format.
Development
Perform the clone-to-launch steps with these terminal commands.
Run demo page in watch mode
git clone https://github.com/ng-select/ng-select
cd ng-select
yarn
yarn run start
Testing
yarn run test
or
yarn run test:watch
Release
To release to npm just run ./release.sh
, of course if you have permissions ;)
Inspiration
This component is inspired by React select and Vitual scroll. Check theirs amazing work and components :)