Skip to content

Instantly share code, notes, and snippets.

@drphrozen
Created November 16, 2012 10:37
Show Gist options
  • Save drphrozen/4086297 to your computer and use it in GitHub Desktop.
Save drphrozen/4086297 to your computer and use it in GitHub Desktop.
AngularJS directives for the document element. Usage doc-click, doc-*. Check ng-click and ng-* for documentation :-)
var dem = angular.module('DocumentEventsModule', []);
angular.forEach(['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout', 'mousemove', 'mouseenter', 'mouseleave'], function(name) {
var capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
var directiveName = 'doc' + capitalizedName;
dem.directive(directiveName, ['$parse', '$document', function(parse, document) {
return function(scope, element, attr) {
var fn = parse(attr[directiveName]);
document.bind(name, function(event) {
scope.$apply(function() {
fn(scope, {$event:event});
});
});
};
}]);
});
@drphrozen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment