Change jQuery Knob Min/Max value

2019-07-06 22:36发布

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条回答
老娘就宠你
2楼-- · 2019-07-06 22:39

Using trigger('configure') followed by trigger('change') should do the trick

$(elem).trigger('configure', {
                  'max': scope.maxbinding[scope.maxbindingprop];
              });
$(elem).trigger('change');

Source

查看更多
登录 后发表回答