title: Not sure how to hide a div when clicking OUTSIDE of the div
body: <p>This is a follow-up question to this question: <a href="https://stackoverflow.com/questions/17678813/angularjs-input-with-focus-kills-ng-repeat-filter-of-list">AngularJS input with focus kills ng-repeat filter of list</a></p>
  
  <p>Basically my code is using AngularJS to pop-out a div (a drawer) on the right for filtering a list of things.  Most times this is used the UI is blocked so clicking on that blocking div closes the drawer.  But in some cases we don't block the UI and need to allow the user to click outside of the drawer to cancel the search or select something else on the page.  </p>
  
  <p>My initial thought was to attach a window.onclick handler when the drawer opens and if anything is clicked other than the drawer it should close the drawer.  That's how I would do it in a pure JavaScript app.  But in Angular it is being a bit more difficult.</p>
  
  <p>Here is a jsfiddle with a sample that I based on @Yoshi's jsBin example: <a href="http://jsfiddle.net/tpeiffer/kDmn8/" rel="noreferrer">http://jsfiddle.net/tpeiffer/kDmn8/</a></p>
  
  <p>The relevant piece of code from this sample is below.  Basically if the user clicks outside of the drawer I invoke $scope.toggleSearch so that $scope.open is set back to false.</p>
  
  <p>The code works, and even though the $scope.open goes from true to false it doesn't modify the DOM.  I'm sure this has something to do with the lifecycle of events or perhaps when I modify $scope (since it is from a separate event) that it is a copy and not the original...</p>
  
  <p>The ultimate goal on this will be for it to be a directive for ultimate reusability.  If anyone can point me in the right direction to do that I would be grateful.</p>
  
  <pre><code>$scope.toggleSearch = function () {
  
      $scope.open = !$scope.open;
  
      if ($scope.open) {
          $scope.$window.onclick = function (event) {
              closeSearchWhenClickingElsewhere(event, $scope.toggleSearch);
          };
      } else {
          $scope.$window.onclick = null;
      }
  };
  </code></pre>
  
  <p>and</p>
  
  <pre><code>function closeSearchWhenClickingElsewhere(event, callbackOnClose) {
  
      var clickedElement = event.target;
      if (!clickedElement) return;
  
      var elementClasses = clickedElement.classList;
      var clickedOnSearchDrawer = elementClasses.contains('handle-right') 
          || elementClasses.contains('drawer-right') 
          || (clickedElement.parentElement !== null 
              &amp;&amp; clickedElement.parentElement.classList.contains('drawer-right'));
      if (!clickedOnSearchDrawer) {
          callbackOnClose();
      }
  
  }
  </code></pre>