angular 如何检测一个按钮被按下了多长时间。

How to detect for how long a button is pressed and hold in angular.

我如何知道按钮被按住了多少时间;即,如何找到 angular 中 mousedown 和 mouseup 事件之间的区别。我是 Javascript 的新手,也是第一次使用 angular。如有任何帮助,我们将不胜感激

您可以使用 ngMouseUpngMouseDown 指令来计算时差,而不是创建单独的指令,如下所示。

希望这就是您所期待的。

angular.module('myApp', [])

.controller('myCtrl', function ($scope){
  var timeStart, timeEnd;

  $scope.mouseDown = function () {
    timeStart = new Date();
  }


  $scope.mouseUp = function () {
   timeEnd = new Date();
   $scope.timeDiff = (timeEnd - timeStart)/1000;
  }  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-mousedown='mouseDown()' ng-mouseup="mouseUp()">Click me and Hold</button>
  <p ng-if="timeDiff"> You have clicked the button for <b>{{timeDiff}}</b> seconds</p>
</div>