This directive allows you to enhance your select elements with behaviour from the select2 library.
- Install karma
$ npm install -g karma - Install bower
$ npm install -g bower - Install components
$ bower install - ???
- Profit!
$ karma start test/test.conf.js --browsers=Chrome
We use bower for dependency management. Add
dependencies: {"angular-ui-select2": "latest"}To your bower.json file. Then run
bower install
This will copy the ui-select2 files into your components folder, along with its dependencies. Load the script files in your application:
<linkrel="stylesheet" href="components/select2/select2.css"><scripttype="text/javascript" src="components/jquery/jquery.js"></script><scripttype="text/javascript" src="components/select2/select2.js"></script><scripttype="text/javascript" src="components/angular/angular.js"></script><scripttype="text/javascript" src="components/angular-ui-select2/src/select2.js"></script>(Note that jquery must be loaded before angular so that it doesn't use jqLite internally)
Add the select2 module as a dependency to your application module:
varmyAppModule=angular.module('MyApp',['ui.select2']);Apply the directive to your form elements:
<selectui-select2ng-model="select2" data-placeholder="Pick a number"><optionvalue=""></option><optionvalue="one">First</option><optionvalue="two">Second</option><optionvalue="three">Third</option></select>All the select2 options can be passed through the directive. You can read more about the supported list of options and what they do on the Select2 Documentation Page
myAppModule.controller('MyController',function($scope){$scope.select2Options={allowClear:true};});<selectui-select2="select2Options" ng-model="select2"><optionvalue="one">First</option><optionvalue="two">Second</option><optionvalue="three">Third</option></select>Some time it may make sense to specify the options in the template file.
<selectui-select2="{ allowClear: true}" ng-model="select2"><optionvalue="one">First</option><optionvalue="two">Second</option><optionvalue="three">Third</option></select>The ui-select2 directive plays nicely with ng-model and validation directives such as ng-required.
If you add the ng-model directive to same the element as ui-select2 then the picked option is automatically synchronized with the model value.
ui-select2 is incompatible with <select ng-options>. For the best results use <option ng-repeat> instead.
<selectui-select2ng-model="select2" data-placeholder="Pick a number"><optionvalue=""></option><optionng-repeat="number in range" value="{{number.value}}">{{number.text}}</option></select>In order to properly support the Select2 placeholder, create an empty <option> tag at the top of the <select> and either set a data-placeholder on the select element or pass a placeholder option to Select2.
<selectui-select2ng-model="number" data-placeholder="Pick a number"><optionvalue=""></option><optionvalue="one">First</option><optionvalue="two">Second</option><optionvalue="three">Third</option></select>If you apply the required directive to element then the form element is invalid until an option is selected.
Note: Remember that the ng-required directive must be explicitly set, i.e. to "true". This is especially true on divs:
<selectui-select2ng-model="number" data-placeholder="Pick a number" ng-required="true"><optionvalue=""></option><optionvalue="one">First</option><optionvalue="two">Second</option><optionvalue="three">Third</option></select>When AngularJS View-Model tags are stored as a list of strings, setting
the ui-select2 specific option simple_tags will allow to keep the model
as a list of strings, and not convert it into a list of Select2 tag objects.
<inputtype="text"
ui-select2="select2Options"
ng-model="list_of_string"
>myAppModule.controller('MyController',function($scope){$scope.list_of_string=['tag1','tag2']$scope.select2Options={'multiple': true,'simple_tags': true,'tags': ['tag1','tag2','tag3','tag4']// Can be empty list.};});