在ng-mousedown中绑定文档mouseup

时间:2015-08-20 05:05:25

标签: javascript jquery angularjs javascript-events mousedown

我希望在ng-mousedown上开始并以mouseup结束。如果鼠标离开元素,我还希望鼠标按钮保持按下状态继续发生。在vanilla javascript中实现此功能的一种非常常见的方法是在元素mousedown事件like so中绑定文档mouseup事件:

HTML:

<button onMouseDown='onMouseDown()'>Click Me</button>
<div id="val"></div>

JS:

function onMouseDown() {
  var el = document.querySelector('#val'),
      changeValue = function() {
        el.innerHTML = 'done!';
        document.removeEventListener('mouseup', changeValue);
      };

  el.innerHTML = 'waiting for mouseup...';

  document.addEventListener('mouseup', changeValue);
}

我在使用jQuery's one()angular.element.oneaddEventListener / removeEventListener的Angular中实现此行为时遇到了问题:

HTML:

<div ng-app="MyApp" ng-controller="AppCtrl">
  <div>Note that 'done!' is not rendered, despite 'mouseup' being logged to the console</div>
  <button ng-mousedown='changeValue()'>Click Me</button>
  <div>{{value}}</div>   
</div>

JS(jQuery&#39; s one()):

angular
  .module('MyApp', [])
  .controller('AppCtrl', function($scope) {
    $scope.changeValue = function() {
      console.log('mousedown');
      $scope.value = 'waiting for mouseup...';  

      $(document).one('mouseup', function() {
        console.log('mouseup');
        $scope.value = 'done!';
      });
    }
  });

JS(angular.element.one):

angular
  .module('MyApp', [])
  .controller('AppCtrl', function($scope) {
    $scope.changeValue = function() {
      console.log('mousedown');
      $scope.value = 'waiting for mouseup...';  

      angular.element(document).one('mouseup', function() {
        console.log('mouseup');
        $scope.value = 'done!';
      });
    }
  });

JS(addEventListener / removeEventListener):

angular
  .module('MyApp', [])
  .controller('AppCtrl', function($scope) {
    $scope.changeValue = function() {
      var changeValue = function() {
        console.log('mouseup');
        $scope.value = 'done!';
        document.removeEventListener('mouseup', changeValue);
      };

      console.log('mousedown');
      $scope.value = 'waiting for mouseup...';  

      document.addEventListener('mouseup', changeValue);
    }
  });

我还尝试利用ng-mouseupng-mouseleave like this

HTML:

<div ng-app="MyApp" ng-controller="AppCtrl">
  <div>While clicking the button, move the mouse off of the button and release the mouse button. Note that 'done!' is not rendered, despite 'mouseup' being logged to the console</div>
  <button ng-mousedown='mouseDown()' ng-mouseup="mouseUp()" ng-mouseleave="mouseLeave()">Click Me</button>
  <div>{{value}}</div>  
</div>

JS:

angular
  .module('MyApp', [])
  .controller('AppCtrl', function($scope) {
    $scope.mouseDown = function() {
      console.log('mousedown');
      $scope.value = 'waiting for mouseup...';  
    }
    $scope.mouseUp = function() {
      console.log('mouseup');
      $scope.value = 'done!';
    }
    $scope.mouseLeave = function() {
      if ($scope.value && $scope.value.indexOf('waiting') > -1) {
        console.log('mouseleave, binding mouseup'); 
        $(document).one('mouseup', function() {
           $scope.mouseUp();
        });
      }
    }
  });

结果是一样的。 $scope.mouseUp已执行,但永远不会呈现'done!'This creates additional problems with $scope.$watch

HTML:

<div ng-app="MyApp" ng-controller="AppCtrl">
  <div>value never === 'done!' in $scope.$watch</div>
  <button ng-mousedown='changeValue()'>Click Me</button>
  <div>{{value}}</div>   
</div>

JS:

angular
  .module('MyApp', [])
  .controller('AppCtrl', function($scope) {
    $scope.changeValue = function() {
      console.log('mousedown');
      $scope.value = 'waiting for mouseup...';  

      $(document).one('mouseup', function() {
        console.log('mouseup');
        $scope.value = 'done!';
      });
    }
    $scope.$watch('value', function(value) {
      if (value === 'done!') {
        console.log('value set to "done!"'); 
      }
    });
  });

1 个答案:

答案 0 :(得分:2)

不知道为什么这个问题在一个多月没有答案的情况下就已经存在了。

这里的主要问题很简单 - 更新$scope.value的代码在AngularJS之外执行。为了使您的数据绑定工作,您应该使用$scope.$apply这样包装它:

$scope.$apply(function () {
  $scope.value = 'done!';                
});

请注意,ng-mouseup工作正常,您可以在按钮上方触发mouseup时看到它。但是,如果您将鼠标移到按钮$(document).one('mouseup'..)之外,则会发生事件。

请记住,如果需要,必须用$scope.$apply包装所有外部回调(JQuery HTTP回调,不与ng- *指令捆绑的DOM事件,setTimeout等)使用AngularJS绑定或观察者。但请三思而后行 - 在另一个$scope.$apply内调用$scope.$apply会导致错误。

您可以浏览documentation或阅读更多here