Skip to content

Commit 83c09f0

Browse files
committed
Merge pull request angular-ui#18 from bmac/add-docs-to-readme
Add some of the information from the docs to the readme.
2 parents 63b2ba7 + d37b740 commit 83c09f0

File tree

1 file changed

+117
-5
lines changed

1 file changed

+117
-5
lines changed

README.md

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
ui-select2 [![Build Status](https://travis-ci.org/angular-ui/ui-select2.png)](https://travis-ci.org/angular-ui/ui-select2)
22
==========
3+
This directive allows you to enhance your select elements with behaviour from the [select2](http://ivaynberg.github.io/select2/) library.
4+
5+
# Requirements
6+
7+
- [AngularJS](http://angularjs.org/)
8+
- [JQuery](http://jquery.com/)
9+
- [Select2](http://ivaynberg.github.io/select2/)
310

411
## Setup
512

6-
1. Install **testacular**
13+
1. Install **testacular**
714
`$ npm install -g testacular@canary`
8-
2. Install **bower**
15+
2. Install **bower**
916
`$ npm install -g bower`
10-
4. Install components
17+
4. Install components
1118
`$ bower install`
1219
4. ???
1320
5. Profit!
@@ -16,6 +23,111 @@ ui-select2 [![Build Status](https://travis-ci.org/angular-ui/ui-select2.png)](
1623

1724
`$ testacular start test/test.conf.js --browsers=Chrome`
1825

19-
## Docs
26+
# Usage
27+
28+
We use [bower](http://twitter.github.com/bower/) for dependency management. Add
29+
30+
```javascript
31+
dependencies: {
32+
"angular-ui-select2": "latest"
33+
}
34+
```
35+
36+
To your `components.json` file. Then run
37+
38+
bower install
39+
40+
This will copy the ui-select2 files into your `components` folder, along with its dependencies. Load the script files in your application:
41+
```html
42+
<script type="text/javascript" src="components/jquery/jquery.js"></script>
43+
<script type="text/javascript" src="components/angular/angular.js"></script>
44+
<script type="text/javascript" src="components/angular-ui-select2/src/select2.js"></script>
45+
```
46+
47+
Add the select2 module as a dependency to your application module:
48+
49+
```javascript
50+
var myAppModule = angular.module('MyApp', ['ui.select2']);
51+
```
52+
53+
Apply the directive to your form elements:
54+
55+
```html
56+
<select ui-select2 ng-model="select2" data-placeholder="Pick a number">
57+
<option value=""></option>
58+
<option value="one">First</option>
59+
<option value="two">Second</option>
60+
<option value="three">Third</option>
61+
</select>
62+
```
63+
64+
## Options
65+
66+
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](http://ivaynberg.github.com/select2/)
67+
68+
```javascript
69+
myAppModule.controller('MyController', function($scope) {
70+
$scope.select2Options = {
71+
allowClear:true
72+
};
73+
});
74+
```
75+
76+
```html
77+
<select ui-select2="select2Options" ng-model="select2">
78+
<option value="one">First</option>
79+
<option value="two">Second</option>
80+
<option value="three">Third</option>
81+
</select>
82+
```
83+
84+
Some time it may make sense to specify the options in the template file.
85+
86+
```html
87+
<select ui-select2="{ allowClear: true}" ng-model="select2">
88+
<option value="one">First</option>
89+
<option value="two">Second</option>
90+
<option value="three">Third</option>
91+
</select>
92+
```
93+
94+
## Working with ng-model
95+
96+
The ui-select2 directive plays nicely with ng-model and validation directives such as ng-required.
97+
98+
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.
99+
100+
## Working with dynamic options
101+
`ui-select2` is incompatible with `<select ng-options>`. For the best results use `<option ng-repeat>` instead.
102+
```html
103+
<select ui-select2 ng-model="select2" data-placeholder="Pick a number">
104+
<option value=""></option>
105+
<option ng-repeat="{{number in range}}" value="{{number.value}}">{{number.text}}</option>
106+
</select>
107+
```
108+
109+
## Working with placeholder text
110+
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.
111+
```html
112+
<select ui-select2 ng-model="number" data-placeholder="Pick a number">
113+
<option value=""></option>
114+
<option value="one">First</option>
115+
<option value="two">Second</option>
116+
<option value="three">Third</option>
117+
</select>
118+
```
119+
120+
## ng-required directive
121+
122+
If you apply the required directive to element then the form element is invalid until an option is selected.
123+
124+
Note: Remember that the ng-required directive must be explicitly set, i.e. to "true". This is especially true on divs:
20125

21-
??
126+
```html
127+
<select ui-select2 ng-model="number" data-placeholder="Pick a number" ng-required="true">
128+
<option value=""></option>
129+
<option value="one">First</option>
130+
<option value="two">Second</option>
131+
<option value="three">Third</option>
132+
</select>
133+
```

0 commit comments

Comments
 (0)