Greg's answer is definitely closest to the cleanest answer. I'm going to build on his answer to come up with a version where no new code needs to be written, and no new injections need to be used in your controller.
The first thing to question is why timeouts are used to hack around these kinds of problems. Essentially, they're used to make a function skip the rest of the current event loop so that the execution is clean. In angular, however, you are actually interested in how the digest loop works. It's almost the same as your classic event handler, except for some minor differences which make it great for UX. Some tools that you have on hand to mess around with the order of function execution include scope.$eval
, scope.$evalAsync
,scope.$apply
, and scope.$applyAsync
.
I believe the $apply
s will kick off another digest loop, which leaves the $eval
s. $eval
will run whatever code you include immediately with the context of the current $scope
and $evalAsync
will queue your function to be run at the end of the digest loop. Essentially, $evalAsync
is a cleaner version of $timeout
with one big difference?—?the first has context and exists on the scope!
This means that you can, actually, handle ng-click
and ng-dblclick
on the same element. Note, however, that this will still trigger the single-click function before the double-click function. This should be sufficient:
<div ng-controller="MyController">
<a href="#"
ng-click="$evalAsync(singleClickAction())"
ng-dblclick="doubleClickAction()">
CLICK
</a>
</div>
Here's a jsfiddle with the intended functionality using Angular 1.6.4.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…