聪明的方法来绕过视野的去抖动?

问题描述:

​​聪明的方法来绕过视野的去抖动?

基本上,我希望我的范围。$手表兑现debounce,但希望一个单独的观察者为我的观点没有反弹。我可以使用任何非肮脏的技术?

<my-slider></my-slider> 

<script type="text/coffeescript"> 
angular.module('myApp', []) 
.directive 'mySlider', -> 
    link = (scope) -> 
    scope.sliderValue = 0 
    scope.$watch 'sliderValue', (value) -> 
     console.log 'Slider Value: ' + value 

    link: link, 
    restrict: 'E', 
    template: 
    'Value: {{sliderValue}}<br>' + 
    '<input type="range" step="1" min="0" max="10" ' + 
    'ng-model="sliderValue" ng-model-options="{ debounce: 1000 }" />' 

angular.bootstrap document, ['myApp'] 
</script> 

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.js"></script> 
+0

“独立观察家对我的看法,而不去抖”?在你的代码中,你正在使用'ng-model-options'来为你的模型设置1秒的“debounce”。现在你想为那个没有“debounce”的模型有一个'watch watch'?你知道这听起来有多矛盾吗?也许你可以使用一个中间的'模型',这首先会挫败'debounce'的目的。例如:从您的 Josep 2014-10-26 17:03:44

+0

@Joseph让心理安抚使用者。没有人喜欢切换窗体控件并获得延迟的反馈,但后端处理不需要执行滑块的每个节拍。这是如何相互矛盾的? – CodeShaman 2014-10-26 18:43:12

+0

“后端处理不需要执行滑块的每个记号” Josep 2014-10-26 19:56:30

我认为,你想要的是这样的:

angular.module("myApp", []).directive "mySlider", ($timeout) -> 
    link = undefined 
    link = (scope) -> 
    debouncePromise = undefined 
    scope.sliderValue = 0 
    scope.$watch "sliderValue", (value, oldVal) -> 
     return unless oldVal 
     $timeout.cancel debouncePromise if debouncePromise 
     debouncePromise = $timeout(-> 

     #your call to the server here! 
     console.log value 
     return 
     , 1000) 
     return 

    return 

    link: link 
    restrict: "E" 
    template: "Value: {{sliderValue}}<br>" + "<input type=\"range\" step=\"1\" min=\"0\" max=\"10\" " + "ng-model=\"sliderValue\" />" 

Example