Last active
November 15, 2015 14:09
-
-
Save vvscode/d94c62d7b33b24b86217 to your computer and use it in GitHub Desktop.
onOutsideClick
This file contains 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
angular | |
.module('smartjs').directive('onOutsideClick', function($parse) { | |
return { | |
restrict: 'A', | |
link: function($scope, $element, $attr) { | |
var element = $element[0]; | |
var fn = function() { | |
$scope.$eval($attr.onOutsideClick); | |
}; | |
var onBodyClickHandler = function(ev) { | |
var targetEl = ev.target; | |
if(element === targetEl || element.contains(targetEl)) { | |
return; | |
} | |
fn(); | |
}; | |
// to prevent handling event which add element to DOM | |
document.body.addEventListener('click', onBodyClickHandler, true); | |
// angular.element(element).ready(function() { | |
// document.body.addEventListener('click', onBodyClickHandler); | |
// }); | |
$element.on('$destroy', function() { | |
document.body.removeEventListener('click', onBodyClickHandler); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment