Created
November 16, 2015 15:44
-
-
Save storuky/eb34f9202ce9801eabe7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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