Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fb7d891

Browse files
bshepherdsonIgorMinar
authored andcommitted
fix(ngMobile): emit click event for touchy clicks
Previously, no handlers for the click event would be called for the fast, touch-based ngMobile clicks, only for desktop browser clicks. Now the event will fire properly for all clicks. Closes #3219 Closes #3218 Closes #3137
1 parent d87fa00 commit fb7d891

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/ngMobile/directive/ngClick.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,7 @@ ngMobile.directive('ngClick', ['$parse', '$timeout', '$rootElement',
233233
}
234234

235235
if (!angular.isDefined(attr.disabled) || attr.disabled === false) {
236-
scope.$apply(function() {
237-
clickHandler(scope, {$event: event});
238-
});
236+
element.triggerHandler('click', event);
239237
}
240238
}
241239

@@ -246,9 +244,12 @@ ngMobile.directive('ngClick', ['$parse', '$timeout', '$rootElement',
246244
// something else nearby.
247245
element.onclick = function(event) { };
248246

249-
// Fallback click handler.
250-
// Busted clicks don't get this far, and adding this handler allows ng-tap to be used on
251-
// desktop as well, to allow more portable sites.
247+
// Actual click handler.
248+
// There are three different kinds of clicks, only two of which reach this point.
249+
// - On desktop browsers without touch events, their clicks will always come here.
250+
// - On mobile browsers, the simulated "fast" click will call this.
251+
// - But the browser's follow-up slow click will be "busted" before it reaches this handler.
252+
// Therefore it's safe to use this directive on both mobile and desktop.
252253
element.on('click', function(event) {
253254
scope.$apply(function() {
254255
clickHandler(scope, {$event: event});

test/ngMobile/directive/ngClickSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,20 @@ describe('ngClick (mobile)', function() {
358358
});
359359

360360

361+
describe('the normal click event', function() {
362+
it('should be capturable by other handlers', inject(function($rootScope, $compile) {
363+
var called = false;
364+
365+
element = $compile('<div ng-click="event = $event" ></div>')($rootScope);
366+
367+
element.on('click', function() {
368+
called = true;
369+
});
370+
371+
browserTrigger(element, 'touchstart', [], 10, 10);
372+
browserTrigger(element, 'touchend', [], 10, 10);
373+
374+
expect(called).toEqual(true);
375+
}));
376+
});
361377
});

0 commit comments

Comments
 (0)