For Angular version 4.x.x and up. Forked from ng2-select2. Supports two-way data-binding.
For this plugin to work you need to add two javascript libraries to your project
First install jQuery using npm
npm i -S jquery
Then install select2 using npm
npm i -S select2
Now include their scrpits and styles in your angularcli.json
file
"styles": [
"styles.css",
"../node_modules/select2/dist/css/select2.min.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.js",
"../node_modules/select2/dist/js/select2.min.js"
],
Add package to your project npm i -S ng-select2
(this will save package to your dependencies
in package.json
)
1) Add declaration to your app.module.ts
import { NgSelect2Module } from 'ng-select2';
@NgModule({
imports: [
....,
NgSelect2Module
],
...
})
2) Add it to your template.
<ng-select2 [data]="exampleData"></ng-select2>
<ng-select2
[(ngModel)]="fruit"
[data]="fruitList"
[placeholder]="'Please select a fruit...'">
</ng-select2>
Array<Select2OptionData>
: Data used for generating select2 - inferface definitionstring
: Default value for select2string
: Set width for the input, default value is resolve
boolean
: Disable select2, default value is false
string
: Placeholder for select2Select2Options
: Set options for select2, all available options for select2string
: Emitted when value changes in select2 drop-down