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

Commit 909b334

Browse files
committed
feat(multicheckbox): Watch model value to support async values
1 parent 26ec0f5 commit 909b334

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

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)