Not watching model change in multiple
piernik opened this issue · comments
Now You have code for watching of external model change:
controller.$render = function () {
getSelection(function (selection) {
if (isMultiple) {
element.select2("data", selection);
} else {
element.select2("val", selection.id);
}
});
};
Problem is that it works only on strings or numbers: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
If we have multiple option turned on and make external change of model nothing happens.
Here is my workaround:
if (isMultiple) {
scope.$watch(function () {
return controller.$modelValue;
}, function (newVal, oldVal) {
if (newVal !== oldVal) {
getSelection(function (selection) {
if (isMultiple) {
element.select2("data", selection);
} else {
element.select2("val", selection.id);
}
});
}
}, true);
} else {
controller.$render = function () {
getSelection(function (selection) {
if (isMultiple) {
element.select2("data", selection);
} else {
element.select2("val", selection.id);
}
});
};
}
If is multiple then use deep watcher - if not stay with $render
EDIT:
I see that initial value for multiple is not working also - controller.$render();
in $timeout
Here You have to trigger:
getSelection(function (selection) {
if (isMultiple) {
element.select2("data", selection);
} else {
element.select2("val", selection.id);
}
});