javascript - Two way data-binding in slider in AngularJS -
i have use case need slider-directive update configuration & model value dynamically through controller. trying create 2 way data binding. however, not able find right way create 2 way data-binding in directive. following code. slide event on slider triggers directive update 'scope.model'. though doesn't respond controller updates 'scope.config' or 'scope.model':
.directive("slider", function() { return { restrict: 'a', scope: { config: "=config", model: "=model", trigger: '=trigger' }, link: function(scope, elem, attrs) { //data binding done here using $watch 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 me in this. have gone through available answers wasn't able find suitable answer
base directive on ngmodelcontroller
. in way can use ngmodel
functionality bind everything, , use ngchange
run callback, without need $watch
(plunkr):
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>
Comments
Post a Comment