title: AngularJS Detect browser back button press in controller?
body: <p>I am making a browser trivia game and I have a view where a authenticated player selects a game type, which emits a socket.io event to be handled by my Node.js server, then changes the path to a "Searching for game" screen with a loading icon as the server waits enough people to join a matchmaking queue.  I put a "Cancel" button on this page, which will emit another socket.io event to my server to remove the user from the matchmaking queue.  </p>
  
  <p>Now here is the problem, if the user presses the back button in their browser, the Cancel button is not pressed so the cancelSearch() function is not called, and therefore the socket.io emit is not sent to the server to remove the user from the search queue.  Here is the relevant code...I believe I will have to add some code to my matchsearchCtrl.js file to be able to handle a back button press and call cancelSearch() accordingly..</p>
  
  <p><strong>playgame.html</strong></p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;h1&gt;Select your Game Type!&lt;/h1&gt;
      &lt;button class="btn btn-default btn-block" ng-click="startMatchMaking()"&gt;1v1&lt;/button&gt;
      &lt;button class="btn btn-default btn-block" ng-click="startMatchMaking()"&gt;2v2&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>playgameCtrl.js</strong></p>
  
  <pre><code>angular.module('myApp')
  .controller('playgameCtrl', function($scope, $rootScope, $uibModal, socket, $location) {
  
      $scope.startMatchMaking = function() {
          socket.emit('joinMatchMaking', $rootScope.user.username);
          $location.path('/searching');
      }
  
  });
  </code></pre>
  
  <p><strong>matchsearch.html</strong></p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;h1&gt;Searching for players...&lt;/h1&gt;
      &lt;button class="btn btn-danger btn-large" ng-click="cancelSearch()"&gt;Cancel&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>matchsearchCtrl.js</strong></p>
  
  <pre><code>angular.module('myApp')
  .controller('matchsearchCtrl', function($scope, $location, socket) {
  
      $scope.cancelSearch = function() {
          socket.emit('leaveMatchMaking', $rootScope.user.username);
          $location.path('/home');
      }
  })
  </code></pre>
  
  <p>And if this is needed...</p>
  
  <p><strong>app.routes.js</strong></p>
  
  <pre><code>myApp.config(function($routeProvider) {
      $routeProvider
      .when('/', {
          templateUrl: './app/views/landingpage.html',
          controller: 'landingpageCtrl'
      })
      .when('/home', {
          templateUrl: './app/views/home.html',
          controller: 'homeCtrl'
      })
      .when('/play', {
          templateUrl: './app/views/playgame.html',
          controller: 'playgameCtrl'
      })
      .when('/searching', {
          templateUrl: './app/views/matchsearch.html',
          controller: 'matchsearchCtrl'
      })
      .otherwise('/');
  });
  </code></pre>