问题
I'm working on an angular wrapper for the jquery-knob widget. The following works as long as the maximum value doesn't change. If it does, the ng-model binding is lost. If I don't destroy the knob widget at the beginning of the watch, the max value doesn't change.
//Directive
app.directive('knobWidget', function () {
return {
scope: {
maxbinding: "=maxbinding",
maxbindingprop: "@maxbindingprop"
},
restrict: 'A',
require: 'ngModel',
link: function (scope, elem, attrs, ngModel) {
ngModel.$render = function () {
$(elem).val(ngModel.$viewValue).trigger("change");
};
scope.$watch('maxbinding', function (newVal) {
$(elem).knob('destroy');
$(elem).knob({
min: 0,
max: scope.maxbinding[scope.maxbindingprop],
value: ngModel.$viewValue,
change: function (changeVal) {
scope.$apply(function () {
ngModel.$setViewValue(changeVal);
});
}
});
});
}
};
});
//Markup
<input knob-widget data-min="0" maxbinding="arr" maxbindingprop="length" ng-model="currentStop" />
Doing:
$(elem).knob('max', scope.maxbinding[scope.maxbindingprop]);
doesn't work either. Any ideas?
回答1:
Using trigger('configure')
followed by trigger('change')
should do the trick
$(elem).trigger('configure', {
'max': scope.maxbinding[scope.maxbindingprop];
});
$(elem).trigger('change');
Source
来源:https://stackoverflow.com/questions/24419582/change-jquery-knob-min-max-value