如何使用 angularjs 鼠标事件在鼠标保持时连续增加值
How to increment value continuously on mouse hold using angularjs mouse events?
我想连续递增值,直到使用 angularjs 将鼠标按住按钮。
<button class="btn btn-primary" ng-click="add('increase');"></button>
$scope.add = function(operation) {
bill.quantity += 1;
}
根据我的理解,什么想要在mouse pressed + hold
上不断增加价值.
您的代码
$scope.add = function(operation) {
bill.quantity += 1;
}
只会在单击时将值增加一个。
对于您想要实现的目标,您必须有 2 个事件。
- 首先,何时开始更新(
ng-mousedown
)。 - 第二,何时停止更新(
ng-mouseup
)。
此外,事件仅触发一次,因此您需要使用setTimeout
及时递增。
此外,更新setTimeout
中的值不会直接反映。你将不得不使用 $scope.$apply()
.参考以下帖子:AngularJS 输入字段未从控制器内的 setTimeout 更新
演示
JSFiddle.
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
var interval = null;
$scope.bill = {
quantity: 0
};
$scope.add = function(newVal) {
console.log("Add");
initInterval(newVal);
}
function initInterval(newVal) {
if (!interval) {
console.log("Interval start");
interval = setInterval(function() {
$scope.$apply(function() {
$scope.bill.quantity += newVal;
});
}, 1000);
}
}
$scope.clearInterval = function() {
console.log("Interval cleared");
if (interval) {
window.clearInterval(interval);
interval = null;
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<div ng-controller="MyCtrl">
<p>{{bill.quantity}}</p>
<button class="btn btn-primary" ng-mousedown="add(1);" ng-mouseup="clearInterval()">Increment</button>
<button class="btn btn-primary" ng-mousedown="add(-1);" ng-mouseup="clearInterval()">Decrement</button>
</div>
相关文章:
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- 当鼠标悬停在文本中的单词上时显示警报
- JsFiddle上的鼠标事件不起作用
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 将鼠标旋转限制为特定的度数
- 跟踪jqplot垂直折线图的鼠标位置
- node-webkit-从父窗口捕获iframe鼠标事件
- 使用CI和CodeDeploy对node.js应用程序进行连续部署
- 悬停下拉菜单即使在鼠标移出后也保持活动状态
- Javascript-在视频中跟踪鼠标位置
- JQuery UI可拖动潜水与滚动棒到鼠标
- 如何在jquery中找到鼠标滚轮/触摸移动事件的走向
- 鼠标按住时连续递增值
- 如何使用 angularjs 鼠标事件在鼠标保持时连续增加值
- Netflix风格的图像连续旋转木马,鼠标悬停,支持Bootstrap
- 连续的鼠标输入功能不起作用
- 连续的鼠标点击事件
- jQuery鼠标悬停连续幻灯片循环