Skip to content

Instantly share code, notes, and snippets.

@storuky
Created November 16, 2015 15:44
Show Gist options
  • Save storuky/eb34f9202ce9801eabe7 to your computer and use it in GitHub Desktop.
Save storuky/eb34f9202ce9801eabe7 to your computer and use it in GitHub Desktop.
app.directive('clickOutside', ['$document', function ($document) {
return {
restrict: 'A',
scope: {
clickOutside: '&'
},
link: function (scope, el, attr) {
var handler = function (e) {
if (el !== e.target && !el[0].contains(e.target) && document.body.contains(e.target)) {
scope.$apply(function () {
scope.$eval(scope.clickOutside);
});
}
}
$document.bind('click', handler);
scope.$on('$destroy', function () {
$document.unbind('click', handler)
})
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment