3

I was trying to figure out how ngClick and ngDblclick work in angularjs. The problem is that even while clicking twice, instead of calling doubleclick function once. Angular calls click function twice and double click function once.

HTML:

  <div ng-controller="MyCtrl">
     <div class="button" ng-click="click()" ng-dblclick="dblclick()">
        Click
     </div>
     <div class="button" ng-click="reset()">
        Reset
     </div>
     <div>
        clicked:{{clicked}}
     </div>
     <div>
        double clicked : {{dblclicked}}
     </div>
</div>

JS:

var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
   $scope.clicked = 0;
   $scope.dblclicked = 0;
   $scope.click = function(){
      $scope.clicked++;
   };
   $scope.dblclick = function(){
      $scope.dblclicked++;
   };
   $scope.reset = function(){
      $scope.clicked = 0;
      $scope.dblclicked = 0;
   };
}

JsFiddle here

But this is not the case in jQuery wherein $(someElement).click() and $(someElement).dblclick() work as expected, which seems to be the ideal way to do it.

I have a fair idea that this is happening because they are implemented as directive in angular. Wherein in jQuery it works by listeners.

I see that you can eliminate this by using $evalAsync, but what reckons me is why we need such an extra burden to achieve something which is very obvious.

Can someone tell me how to handle the scenario. Thanks in advance.

Ragul Parani
  • 621
  • 7
  • 22
  • 1
    intresting... could this help https://github.com/angular/angular.js/issues/9826 ? – Ovidiu Dolha Jan 09 '17 at 14:08
  • 2
    Possible duplicate of [Handling ng-click and ng-dblclick on the same element with AngularJS](http://stackoverflow.com/questions/20444409/handling-ng-click-and-ng-dblclick-on-the-same-element-with-angularjs) – JJJ Jan 09 '17 at 14:08
  • @OvidiuDolha the links which you suggested was something quite opposite to what i want to achieve. Shouldn't the click be prevented when i am trying to Double click. – Ragul Parani Jan 09 '17 at 14:20
  • 1
    https://github.com/angular/angular.js/issues/4322 - it's been discussed by angular team – Andrey Jan 09 '17 at 14:26

0 Answers0