angular.module('offClick',[])
.directive('offClick', ['$document', '$timeout', function ($document, $timeout) {
        
    function targetInFilter(target,filter){
        if(!target || !filter) return false;
        var elms = angular.element(document.querySelectorAll(filter));
        var elmsLen = elms.length;
        for (var i = 0; i< elmsLen; ++i)
            if(elms[i].contains(target)) return true;
        return false;
    }
    
    return {
        restrict: 'A',
        scope: {
            offClick: '&',
            offClickIf: '&'
        },
        link: function (scope, elm, attr) {

            if (attr.offClickIf) {
                scope.$watch(scope.offClickIf, function (newVal, oldVal) {
                        if (newVal && !oldVal) {
                            $timeout(function() {
                                $document.on('click', handler);
                            });
                        } else if(!newVal){
                            $document.off('click', handler);
                        }
                    }
                );
            } else {
                $document.on('click', handler);
            }

            scope.$on('$destroy', function() {
                $document.off('click', handler);
            });

            function handler(event) {
                // This filters out artificial click events. Example: If you hit enter on a form to submit it, an
                // artificial click event gets triggered on the form's submit button.
                if (event.pageX == 0 && event.pageY == 0) return;
                
                var target = event.target || event.srcElement;
                if (!(elm[0].contains(target) || targetInFilter(target, attr.offClickFilter))) {
                    scope.$apply(scope.offClick());
                }
            }
        }
    };
}]);

@@ -105,25 +105,17 @@
 eout) %7B%0A
-        
 %0A
+
     func
@@ -424,21 +424,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     retu
@@ -1263,16 +1263,168 @@
    %7D);%0A%0A
+            var offClickFilter;%0A            attrs.$observe('offClickFilter', function(value) %7B%0A                offClickFilter = value;%0A            %7D);%0A%0A
         
@@ -1723,25 +1723,9 @@
 rn;%0A
-                
 %0A
+
     
@@ -1783,16 +1783,16 @@
 lement;%0A
+
         
@@ -1855,21 +1855,16 @@
 target, 
-attr.
 offClick

support interpolations in offClickFilter