Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit ee00911

Browse files
committed
Parse repeat attribute
- Allows to specify something else than "item" inside the repeat expression - Allows to specify a promise inside the repeat expression - Move out ui-select controller from the directive - Various fixes and refactoring
1 parent e7ef5ee commit ee00911

File tree

12 files changed

+664
-201
lines changed

12 files changed

+664
-201
lines changed

README.md

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
AngularJS-native version of [Select2](http://ivaynberg.github.io/select2/) and [Selectize](http://brianreavis.github.io/selectize.js/).
44

5-
- [Basic demo](http://plnkr.co/edit/GtOOWE?p=preview)
5+
- [Demo](http://plnkr.co/edit/GtOOWE?p=preview)
66
- [Bootstrap theme](http://plnkr.co/edit/7Fpbtg?p=preview)
77

88
## Features
@@ -11,7 +11,7 @@ AngularJS-native version of [Select2](http://ivaynberg.github.io/select2/) and [
1111
- Available themes: Bootstrap, Select2 and Selectize
1212
- Keyboard support
1313
- jQuery not required (except for old browsers)
14-
- Small code base: 250 lines of JavaScript vs 20 KB for select2.min.js
14+
- Small code base: 400 lines of JavaScript vs 20 KB for select2.min.js
1515

1616
For the roadmap, check [issue #3](https://github.com/angular-ui/ui-select/issues/3) and the [Wiki page](https://github.com/angular-ui/ui-select/wiki/Roadmap).
1717

@@ -28,6 +28,25 @@ Check the [examples](https://github.com/angular-ui/ui-select/blob/master/example
2828
- select.js: `<script src="bower_components/ui-select/dist/select.js"></script>`
2929
- select.css: `<link rel="stylesheet" href="bower_components/ui-select/dist/select.css">`
3030

31+
### Bootstrap theme
32+
33+
If you already use Bootstrap, this theme will save you a lot of CSS code compared to the Select2 and Selectize themes.
34+
35+
Bower:
36+
- `bower install bootstrap`
37+
- `<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">`
38+
- Or the [LESS](http://lesscss.org/) version: `@import "bower_components/bootstrap/less/bootstrap.less";`
39+
40+
[Bootstrap CDN](http://www.bootstrapcdn.com/):
41+
- `<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">`
42+
43+
Configuration:
44+
```JavaScript
45+
app.config(function(uiSelectConfig) {
46+
uiSelectConfig.theme = 'bootstrap';
47+
});
48+
```
49+
3150
### Select2 theme
3251

3352
Bower:
@@ -61,23 +80,62 @@ app.config(function(uiSelectConfig) {
6180
});
6281
```
6382

64-
### Bootstrap theme
83+
## FAQ
6584

66-
If you already use Bootstrap, this theme will save you a lot of CSS code compared to the Select2 and Selectize themes.
85+
### ng-model not working with a simple variable on $scope
6786

68-
Bower:
69-
- `bower install bootstrap`
70-
- `<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">`
71-
- Or the [LESS](http://lesscss.org/) version: `@import "bower_components/bootstrap/less/bootstrap.less";`
87+
You cannot write:
88+
```HTML
89+
<ui-select ng-model="item"> <!-- Wrong -->
90+
[...]
91+
</ui-select>
92+
```
7293

73-
[Bootstrap CDN](http://www.bootstrapcdn.com/):
74-
- `<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">`
94+
You need to write:
95+
```HTML
96+
<ui-select ng-model="item.selected"> <!-- Correct -->
97+
[...]
98+
</ui-select>
99+
```
100+
101+
Or:
102+
```HTML
103+
<ui-select ng-model="$parent.item"> <!-- Hack -->
104+
[...]
105+
</ui-select>
106+
```
107+
108+
For more explanations, check [ui-select #18](https://github.com/angular-ui/ui-select/issues/18) and [angular.js #6199](https://github.com/angular/angular.js/issues/6199).
109+
110+
### ng-bind-html gives me "Error: [$sce:unsafe] Attempting to use an unsafe value in a safe context"
111+
112+
You need to use module [ngSanitize](http://docs.angularjs.org/api/ngSanitize) (recommended) or [$sce](http://docs.angularjs.org/api/ng/service/$sce):
75113

76-
Configuration:
77114
```JavaScript
78-
app.config(function(uiSelectConfig) {
79-
uiSelectConfig.theme = 'bootstrap';
80-
});
115+
$scope.trustAsHtml = function(value) {
116+
return $sce.trustAsHtml(value);
117+
};
118+
```
119+
120+
```HTML
121+
<div ng-bind-html="trustAsHtml((item | highlight: $select.search))"></div>
122+
```
123+
124+
### I get "TypeError: Object [...] has no method 'indexOf' at htmlParser"
125+
126+
You are using ng-bind-html with a number:
127+
```HTML
128+
<div ng-bind-html="person.age | highlight: $select.search"></div>
129+
```
130+
131+
You should write instead:
132+
```HTML
133+
<div ng-bind-html="''+person.age | highlight: $select.search"></div>
134+
```
135+
136+
Or:
137+
```HTML
138+
<div ng-bind-html="person.age.toString() | highlight: $select.search"></div>
81139
```
82140

83141
## Run the tests
@@ -90,7 +148,7 @@ bower update # Installs all ui-select dependencies (bower.json) inside bower_com
90148

91149
To run the tests:
92150
```
93-
grunt build # Build dist/select.js
151+
grunt build # Builds dist/select.js
94152
grunt test # Launches Karma
95153
```
96154

@@ -99,4 +157,4 @@ grunt test # Launches Karma
99157
- Run the tests
100158
- Try the [examples](https://github.com/angular-ui/ui-select/blob/master/examples)
101159

102-
When issuing a pull request, please exclude changes in the "dist" folder to avoid merge conflicts.
160+
When issuing a pull request, please exclude changes from the "dist" folder to avoid merge conflicts.

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"devDependencies": {
2222
"jquery": "~1.11",
23+
"angular-sanitize": "~1.2",
2324
"angular-mocks": "~1.2"
2425
}
2526
}

examples/bootstrap.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
</script>
1919
<![endif]-->
2020

21-
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.js"></script>
21+
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js"></script>
22+
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-sanitize.js"></script>
2223
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">
2324

2425
<!-- ui-select files -->
@@ -32,7 +33,7 @@
3233
</style>
3334
</head>
3435

35-
<body ng-controller="MainCtrl">
36+
<body ng-controller="DemoCtrl">
3637
<script src="demo.js"></script>
3738

3839
<p>Selected: {{person.selected.name}}</p>
@@ -48,8 +49,8 @@
4849
<ui-select ng-model="person.selected" theme="bootstrap">
4950
<match placeholder="Select or search a person in the list...">{{$select.selected.name}}</match>
5051
<choices repeat="item in people | filter: $select.search">
51-
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div>
52-
<small ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></small>
52+
<div ng-bind-html="item.name | highlight: $select.search"></div>
53+
<small ng-bind-html="item.email | highlight: $select.search"></small>
5354
</choices>
5455
</ui-select>
5556

@@ -64,8 +65,8 @@
6465
<ui-select ng-model="person.selected" theme="bootstrap">
6566
<match placeholder="Select or search a person in the list...">{{$select.selected.name}}</match>
6667
<choices repeat="item in people | filter: $select.search">
67-
<span ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></span>
68-
<small ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></small>
68+
<span ng-bind-html="item.name | highlight: $select.search"></span>
69+
<small ng-bind-html="item.email | highlight: $select.search"></small>
6970
</choices>
7071
</ui-select>
7172

@@ -86,8 +87,8 @@
8687
<ui-select ng-model="person.selected" theme="bootstrap" ng-disabled="true">
8788
<match placeholder="Select or search a person in the list...">{{$select.selected.name}}</match>
8889
<choices repeat="item in people | filter: $select.search">
89-
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div>
90-
<small ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></small>
90+
<div ng-bind-html="item.name | highlight: $select.search"></div>
91+
<small ng-bind-html="item.email | highlight: $select.search"></small>
9192
</choices>
9293
</ui-select>
9394

examples/demo.html

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
</script>
1919
<![endif]-->
2020

21-
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.js"></script>
21+
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js"></script>
22+
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-sanitize.js"></script>
2223
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">
2324

2425
<!-- ui-select files -->
@@ -54,37 +55,42 @@
5455
</style>
5556
</head>
5657

57-
<body ng-controller="MainCtrl">
58+
<body ng-controller="DemoCtrl">
5859
<script src="demo.js"></script>
5960

60-
<p>Selected: {{person.selected.name}}</p>
61+
<button class="btn btn-default btn-xs" ng-click="enable()">Enable ui-select</button>
62+
<button class="btn btn-default btn-xs" ng-click="disable()">Disable ui-select</button>
63+
<button class="btn btn-default btn-xs" ng-click="clear()">Clear ng-model</button>
6164

62-
<p>Select2 theme:</p>
63-
<ui-select ng-model="person.selected" theme="select2" style="width: 300px;">
64-
<match placeholder="Select or search a person in the list...">{{$select.selected.name}}</match>
65-
<choices repeat="item in people | filter: $select.search">
66-
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div>
67-
<small ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></small>
65+
<h3>Bootstrap theme</h3>
66+
<p>Selected: {{address.selected.formatted_address}}</p>
67+
<ui-select ng-model="address.selected" theme="bootstrap" ng-disabled="disabled" style="width: 300px;">
68+
<match placeholder="Enter an address...">{{$select.selected.formatted_address}}</match>
69+
<choices repeat="address in getAddress($select.search) track by $index">
70+
<div ng-bind-html="address.formatted_address | highlight: $select.search"></div>
6871
</choices>
6972
</ui-select>
70-
<br><br>
7173

72-
<p>Selectize theme:</p>
73-
<ui-select ng-model="person.selected" theme="selectize" style="width: 300px;">
74-
<match placeholder="Select or search a person in the list...">{{$select.selected.name}}</match>
75-
<choices repeat="item in people | filter: $select.search">
76-
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div>
77-
<small ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></small>
74+
<h3>Select2 theme</h3>
75+
<p>Selected: {{person.selected}}</p>
76+
<ui-select ng-model="person.selected" theme="select2" ng-disabled="disabled" style="width: 300px;">
77+
<match placeholder="Select a person in the list or search his age...">{{$select.selected.name}}</match>
78+
<choices repeat="person in people | filter: {age: $select.search}">
79+
<div ng-bind-html="person.name | highlight: $select.search"></div>
80+
<small>
81+
email: {{person.email}}
82+
age: <span ng-bind-html="''+person.age | highlight: $select.search"></span>
83+
</small>
7884
</choices>
7985
</ui-select>
80-
<br>
8186

82-
<p>Bootstrap theme:</p>
83-
<ui-select ng-model="person.selected" theme="bootstrap" style="width: 300px;">
84-
<match placeholder="Select or search a person in the list...">{{$select.selected.name}}</match>
85-
<choices repeat="item in people | filter: $select.search">
86-
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div>
87-
<small ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></small>
87+
<h3>Selectize theme</h3>
88+
<p>Selected: {{country.selected}}</p>
89+
<ui-select ng-model="country.selected" theme="selectize" ng-disabled="disabled" style="width: 300px;">
90+
<match placeholder="Select or search a country in the list...">{{$select.selected.name}}</match>
91+
<choices repeat="country in countries | filter: $select.search">
92+
<span ng-bind-html="country.name | highlight: $select.search"></span>
93+
<small ng-bind-html="country.code | highlight: $select.search"></small>
8894
</choices>
8995
</ui-select>
9096
</body>

0 commit comments

Comments
 (0)