This question is related to a previous question which is no longer looked at because it has an upvoted answer: Change jQuery Knob Min/Max value
The following code is a wrapper for the jQuery knob plugin. I am aware of existing angular wrappers, but am using this to further my understanding of the angular framework. Anyway, so my problem is that when the $watch()'s detect changes to the min and max values for the knob, the triggered change event causes some kind of collision within angular, resulting in a $digest already in progress error. Commenting out the two lines within the change function doesn't stop the error, but commenting out the $(elem).trigger('change'); lines does. This breaks my initial hypothesis that the scope.$apply() was the cause. If anyone has any idea what is happening here, any information would be appreciated. Thank you.
App.directive('knobWidget', function () {
return {
scope: {
minbinding: "=minbinding",
minbindingprop: "@minbindingprop",
maxbinding: "=maxbinding",
maxbindingprop: "@maxbindingprop",
delta: "@delta"
},
restrict: 'A',
require: 'ngModel',
link: function (scope, elem, attrs, ngModel) {
ngModel.$render = function () {
$(elem).val(ngModel.$viewValue)
$(elem).trigger("change");
};
$(elem).knob({
min: 0,
max: 1,
value: ngModel.$viewValue,
change: function (changeVal) {
ngModel.$setViewValue(changeVal);
scope.$apply();
}
});
scope.$watch(function () { return scope.maxbinding[scope.maxbindingprop] }, function (newVal) {
$(elem).trigger('configure', { 'max': scope.maxbinding[scope.maxbindingprop] + parseInt(scope.delta) });
$(elem).trigger('change');
});
scope.$watch(function () { return scope.minbinding[scope.minbindingprop] }, function (newVal) {
$(elem).trigger('configure', { 'min': scope.minbinding[scope.minbindingprop] + parseInt(scope.delta) });
$(elem).trigger('change');
});
}
};
});