Reputation: 9721
I want to implement a Push To Talk button that enables voice when I mouse down, and disables voice when I release the mouse. Here's the code I have right now:
<button ng-mousedown="enablePushToTalk()" ng-mouseup="disablePushToTalk()" class="btn"> PushToTalk </button>
The problem scenario is this:
Here's a gif:
How do I ensure that "disablePushToTalk" gets called for every time that "enablePushToTalk" is called?
Upvotes: 0
Views: 1102
Reputation: 3892
How about adding an ng-mouseup
event listener to a parent element?
Created a jsBin using above idea for your problem.
Solution 1:
//html
<body style='height:500px;border:1px solid black;' ng-controller="MyCtrl" ng-mouseup='disable()'>
<div >
<button ng-mousedown="enablePushToTalk()" ng-mouseup="disablePushToTalk()" class="btn"> PushToTalk </button>
</div>
</body>
//js
function MyCtrl($scope) {
$scope.name = 'Superhero';
var isMouseUp = false;
$scope.enablePushToTalk = function(){console.log('mouseup');isMouseUp=true;}
$scope.disablePushToTalk = function(){console.log('mousedown');isMouseUp=false;}
$scope.disable = function(){if(!isMouseUp) return;console.log('mousedown from body');isMouseUp=false;}
}
jsBin link: http://jsbin.com/nedijudaga/edit?html,js,console,output
Solution 2:
Adding an ng-mouseup
event only to parent element and not on button
would do the trick.
Upvotes: 1