javascript - AngularJS Ion.RangeSlider 不会在更改特定按钮时动态更新最小值和最大值

标签 javascript jquery html angularjs rangeslider

我有一个在分析间使用的 slider 指令 ionRangeSlider。单击按钮时,混合值和最大值会发生变化 但它没有在指令中更新。我在中添加了 watch 功能 链接。

下面是 slider 指令

var app = angular.module('ionSlider',[]);
app.directive('ionslider',function($timeout){
return{
    restrict:'AE',
    require: 'ngModel',
    scope:{
    options:'='
    },
    template:'<div id="{{id}}" ></div>',
    replace:true,
    link:function($scope, $element, attrs, ngModel) {
       // //function init(){
        var init =function(){
            $element.ionRangeSlider({
                min: $scope.options.min,
                max: $scope.options.max,
                type: $scope.options.type,
                prefix: $scope.options.prefix,
                maxPostfix: $scope.options.maxPostfix,
                prettify: $scope.options.prettify,
                hasGrid: $scope.options.hasGrid,
                gridMargin: $scope.options.gridMargin,
                postfix:$scope.options.postfix,
                from:$scope.options.from,
                step:$scope.options.step,
                hideMinMax:$scope.options.hideMinMax,
                hideFromTo:$scope.options.hideFromTo,
                onChange:$scope.options.onChange
            });
        };
        init();
      //OnChange
     var    update = function()
        {
         $element.ionRangeSlider ({
              min: $scope.options.min,
                max: $scope.options.max,
                type: $scope.options.type,
                prefix: $scope.options.prefix,
                maxPostfix: $scope.options.maxPostfix,
                prettify: $scope.options.prettify,
                hasGrid: $scope.options.hasGrid,
                gridMargin: $scope.options.gridMargin,
                postfix:$scope.options.postfix,
                from:$scope.options.from,
                step:$scope.options.step,
                hideMinMax:$scope.options.hideMinMax,
                hideFromTo:$scope.options.hideFromTo,
                onChange:$scope.options.onChange
         });

        };
    //watch
      $scope.$watch('options', function(value) {
        $timeout(function(){ update(); }); 

      });
     }
    }
});

其 HTML 代码如下:

    <td  style="width:30%;" class="normal_row"><span><input ng-model="ldcInput.value" type="text" id={{key}} ionslider options="{'min':ldcInput.minValue,'max':ldcInput.maxValue,'step':ldcInput.step}" ng-change="mathCalculation(ldcInput)"/></span></td>

上面的最小值和最大值 slider 值会根据单击按钮而更改。但它没有反射(reflect)在 slider 中。请让我知道指令中哪里出错了。

最佳答案

由于 ion.RangeSlider 是由 jQuery 注入(inject)的,因此您无法通过再次调用注入(inject)来更新其任何属性。

我通过向此分支添加可更新的属性和一些缺失的属性来改进现有的指令,这将解决您的问题。有关如何使用它的示例,请参阅 github 页面。

/**
 * Created by Abdullah on 9/19/14.
 * 
 * Modified and enhanced by Juergen Wahlmann on 3/5/15
 */

var app = angular.module('ionSlider',['ngRoute']);


app.directive('ionslider',function($timeout){
return{
    restrict:'E',
    scope:{min:'=',
        max:'=',
        type:'@',
        prefix:'@',
        maxPostfix:'@',
        prettify:'@',
        hasGrid:'@',
        gridMargin:'@',
        postfix:'@',
        step:'@',
        hideMinMax:'@',
        hideFromTo:'@',
        from:'=',
        disable:'=',
        onChange:'=',
        onFinish:'='

    },
    template:'<div></div>',
    replace:true,
    link:function($scope,$element,attrs){
        (function init(){
            $($element).ionRangeSlider({
                min: $scope.min,
                max: $scope.max,
                type: $scope.type,
                prefix: $scope.prefix,
                maxPostfix: $scope.maxPostfix,
                prettify: $scope.prettify,
                hasGrid: $scope.hasGrid,
                gridMargin: $scope.gridMargin,
                postfix:$scope.postfix,
                step:$scope.step,
                hideMinMax:$scope.hideMinMax,
                hideFromTo:$scope.hideFromTo,
                from:$scope.from,
                disable:$scope.disable,
                onChange:$scope.onChange,
                onFinish:$scope.onFinish
            });
        })();
        $scope.$watch('min', function(value) {
            $timeout(function(){ $($element).data("ionRangeSlider").update({min: value}); });
        },true);
        $scope.$watch('max', function(value) {
            $timeout(function(){ $($element).data("ionRangeSlider").update({max: value}); });
        });
        $scope.$watch('from', function(value) {
            $timeout(function(){ $($element).data("ionRangeSlider").update({from: value}); });
        });
        $scope.$watch('disable', function(value) {
            $timeout(function(){ $($element).data("ionRangeSlider").update({disable: value}); });
        });
    }
}
});

ionRangeSlider-Angular-Directive

关于javascript - AngularJS Ion.RangeSlider 不会在更改特定按钮时动态更新最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28065146/

相关文章:

javascript - react : Unselect all radio buttons via button click

javascript - Highcharts - 更新系列会导致与工具提示相关的错误(鼠标悬停)

javascript - 将 XML 解析为表格适用于除 IE 之外的所有浏览器

javascript - 使用 Javascript 打印外部 HTML 文件

html - 即使不需要滚动条也可见 -

javascript - 如何让鼠标悬停事件在 p 和父 div 上都起作用

javascript - Laravel,PHP - 返回我的 Blade View 时出错

jquery - 用jquery获取固定div的位置

jquery - 在 fancyBox v3 上启用全屏 Vimeo 视频按钮

javascript - 伪造 CSS :only-child in IE8 with jQuery/JavaScript