title: In AngularJS, how to detect when user leaves template/page?
body: <p>I am using the Javascript command:  setInterval.  I like to stop it when the user leaves the page.</p>
  
  <p>This code seems to work well:  <a href="http://jsfiddle.net/PQz5k/" rel="noreferrer">http://jsfiddle.net/PQz5k/</a></p>
  
  <p>It detects when a user leaves a page.  It executes Javascript code when a user clicks on a link to go to a different HTML page or URL, or if user reloads page.</p>
  
  <p>However, it does not work when I go from one AngularJS template to another.  As an example, if I am at template1.html, I want the Javascript code to do something in Controller1.js when the user leaves template1.html to go to template2.html.  What is the equivalent of this code below in AngularJS?:</p>
  
  <pre><code>$(window).on('beforeunload', function() {
      return 'Your own message goes here...';
  });​
  </code></pre>