Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 134dbe0

Browse files
author
Kent C. Dodds
committed
Merge pull request #33 from m0t0r/multicheckbox-watch-model
Watch multiCheckbox model value to support async options
2 parents 26ec0f5 + 596ad75 commit 134dbe0

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,52 @@ _Example checkbox field_
154154
}
155155
```
156156

157+
---
158+
#### multiCheckbox form field
159+
>The multiCheckbox field allows to have a set of checkboxes which will be bind to a provided model value.
160+
161+
##### options (array, required)
162+
>`options` is an array of options for the multiCheckbox form field to display. Each option should be an object.
163+
164+
##### labelProp (string, optional)
165+
>`labelProp` is what is used for what is shown to the user. Defaults to `name`
166+
167+
##### valueProp (string, optional)
168+
>`valueProp` is what is used for the value assigned to the model. Defaults to `value`
169+
170+
_Example multiCheckbox field_
171+
```json
172+
{
173+
key: 'roles',
174+
type: 'multiCheckbox',
175+
templateOptions: {
176+
label: 'Roles',
177+
options: [{id: 1, title : "Administrator"}, {id: 2, title : "User"}],
178+
valueProp: 'id',
179+
labelProp: 'title'
180+
}
181+
}
182+
```
183+
184+
_Example multiCheckbox field with async options_
185+
```javascript
186+
{
187+
key: 'roles',
188+
type: 'multiCheckbox',
189+
templateOptions: {
190+
label: 'Roles',
191+
options: [],
192+
valueProp: 'id',
193+
labelProp: 'title'
194+
},
195+
controller: function($scope, DataService) {
196+
DataService.getRoles().then(function(roles){
197+
// roles: [{id: 1, title : "Administrator"}, {id: 2, title : "User"}]
198+
$scope.to.options = roles;
199+
});
200+
}
201+
}
202+
```
157203
---
158204
#### Radio form field
159205
>The radio field allows multiple choice input with a series of linked inputs, with `type='radio'`.

src/types/multiCheckbox.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,22 @@ export default ngModule => {
3333
};
3434

3535
// initialize the checkboxes check property
36-
const modelValue = $scope.model[opts.key];
37-
if (angular.isArray(modelValue)) {
38-
const valueProp = to.valueProp || 'value';
39-
angular.forEach(to.options, function(v, index) {
40-
$scope.multiCheckbox.checked[index] = modelValue.indexOf(v[valueProp]) !== -1;
41-
});
42-
}
36+
$scope.$watch('model', function modelWatcher(newModelValue) {
37+
var modelValue, valueProp;
38+
39+
if(Object.keys(newModelValue).length) {
40+
modelValue = newModelValue[opts.key];
41+
42+
$scope.$watch('to.options', function optionsWatcher(newOptionsValues) {
43+
if(newOptionsValues && Array.isArray(newOptionsValues) && Array.isArray(modelValue)) {
44+
valueProp = to.valueProp || 'value';
45+
for (var index = 0; index < newOptionsValues.length; index++) {
46+
$scope.multiCheckbox.checked[index] = modelValue.indexOf(newOptionsValues[index][valueProp]) !== -1;
47+
}
48+
}
49+
});
50+
}
51+
}, true);
4352

4453
function checkValidity(expressionValue){
4554
var valid = angular.isArray($scope.model[opts.key]) &&

0 commit comments

Comments
 (0)