1

私は、ionRangeSlider を相互に使用しているスライダーのディレクティブを持っています。ボタンをクリックすると、ミックスと最大値が変更されますが、ディレクティブでは更新されません。リンクにウォッチ機能を追加しました。

以下はスライダーディレクティブです

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>

上記のスライダーの最小値と最大値は、ボタンのクリックに基づいて変更されます。ですが、スライダーには反映されません。ディレクティブのどこが間違っているのか教えてください。

4

1 に答える 1

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-ディレクティブ

于 2015-03-05T12:15:52.230 に答える