First make sure you have Modernizr included in your project. I created a simple provider that will allow Modernizr to be injected into our directive rather than accessed globally. We use Modernizr to detect if we are on a touch device later on in the directive.
http://joegaudet.com/angular/mobile/2013/07/22/fast-clicks-with-angular-js.html
angular.module('fast-click').provider('Modernizr', function () {
'use strict';
this.$get = function () {
return Modernizr || {};
};
});
Next let's define our fast click directive
angular.module('fast-click').directive('fastClick', function ($parse, Modernizr) {
'use strict';
return {
restrict: 'A',
link: function (scope, element, attrs) {
}
};
});
You'll notice we've injected two dependencies into our fast-click directive. The first is $parse
, which converts the Angular
expression that is passed to the directive into a function - this
snippet is taken from the ng-click
directive which ships with Angular,
we wrap it in our own function for a bit of DRYness
clickFunction = function (event) {
// if something has caused this handler to be
// canceled lets prevent execution
if (!canceled) {
scope.$apply(function () {
fn(scope, {$event: event});
});
}
};