简体   繁体   English

AngularJS中的滑块中的两种数据绑定

[英]Two way data-binding in slider in AngularJS

I have a use case where I need the slider-directive to update configuration & model value dynamically through a controller. 我有一个用例,我需要滑块指令来通过控制器动态更新配置和模型值。 For this I am trying to a create a two way data binding. 为此,我试图创建一种双向数据绑定。 However, I am not able to find the right way to create two way data-binding in directive. 但是,我找不到在指令中创建双向数据绑定的正确方法。 Following is the code. 以下是代码。 Currently an slide event on slider triggers directive to update 'scope.model'. 当前,滑块上的滑动事件触发指令以更新“ scope.model”。 Though it doesn't respond to controller updates for 'scope.config' or 'scope.model': 尽管它不响应“ scope.config”或“ scope.model”的控制器更新:

.directive("slider", function() {
return {
    restrict: 'A',
    scope: {
        config: "=config",
        model: "=model",
        trigger: '=trigger'
    },
    link: function(scope, elem, attrs) {

     //Data Binding to be done here using $watch for scope.model

        $(elem).slider({
            range: "min",
            min: scope.config.min,
            max: scope.config.max,
            value: scope.model,
            step: scope.config.step,
            slide: function(event, ui) {
                scope.$apply(function() {
                    scope.model = ui.value;
                });
                console.log(scope.model);
                scope.$apply(scope.trigger);
            }
            });
        }
    }
});

Please help me in this. 请帮助我。 I have gone through all the available answers but wasn't able to find the suitable answer 我已经查看了所有可用的答案,但找不到合适的答案

Base your directive on ngModelController . 您的指令基于ngModelController In this way you can use the ngModel functionality to bind everything, and use ngChange to run callback, without the need for $watch ( plunkr ): 这样,您可以使用ngModel功能绑定所有内容,并使用ngChange运行回调,而无需$watchplunkr ):

app.directive("slider", function() {
  return {
    restrict: 'A',
    scope: {
      config: "=config"
    },
    require: 'ngModel',
    link: function(scope, elem, attrs, ngModel) {
      var init = false;
      var $slider = $(elem);

      ngModel.$render = function() {
        if (!init) {
          $slider.slider({
            range: "min",
            min: scope.config.min,
            max: scope.config.max,
            step: scope.config.step,
            slide: function(event, ui) {
              scope.$apply(function() {
                ngModel.$setViewValue(ui.value);
              });
            }
          });

          init = true;
        }

        $slider.slider('value', parseInt(ngModel.$viewValue, 10));
      }

    }
  }
});

Usage: 用法:

  <div id="km-slider" class="slider" slider="" config="milesSliderConfig" 
  ng-change="filterCars(sliderRanges.kms)"
  ng-model="sliderRanges.kms"></div>

I have figured out the solution to this by using $watch 我已经通过使用$ watch找到了解决方案

//watch the Model to set slider when val in Model var changes
scope.$watch('model', function(newVal, oldVal){
   //check when Model is not initialized
   if (newVal !== undefined){
      elem.slider("value", parseInt(newVal,10));
       }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM