【问题标题】:AngularJS Ion.RangeSlider not updating the min & max dynamically on change of a particalar buttonAngularJS Ion.RangeSlider 不会在更改特定按钮时动态更新最小值和最大值
【发布时间】:2015-01-21 10:42:55
【问题描述】:

我有一个内部使用的滑块指令 离子范围滑块。单击按钮时,混合值和最大值会更改 但它没有在指令中更新。我在 链接。

下面是滑块指令

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>

上面的 min 和 max 滑块值会根据按钮的点击而改变。但它没有反映在滑块中。请让我知道我在指令中哪里出错了。

【问题讨论】:

    标签: javascript jquery html angularjs rangeslider


    【解决方案1】:

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

    我通过向这个分支添加可更新属性和一些缺失的属性来改进已经存在的指令,这将解决您的问题。有关如何使用它的示例,请参阅 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

    【讨论】:

    • 嘿 Jurgen - 我知道你在某个时候发过这个。 prettify 似乎错误地命名为 prettify:'@',而不是 prettify:'=' ...无论如何我似乎都无法启动 prettify。
    • @Jonathan 不幸的是,我无法再访问这个项目,所以我无法检查或测试任何与之相关的内容。我希望你能解决它。
    • 感谢您的回复。我更新了我的 iorangeslider 版本并修复了上述问题,现在一切正常。感谢您的回复!
    猜你喜欢
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2012-02-06
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多