title: AngularJs : event listener on http requests
body: <p>Since i'm using Oauth2 to protect my Api, i need to get a new access token before any http requets if the previous access token has expired.</p>
  
  <p>I didn't used event listener much until now.</p>
  
  <p>Here what i did for now (Please let me know if it is correct) : </p>
  
  <p><strong>ApplicationController.js</strong> :</p>
  
  <pre><code>app.controller('ApplicationController', function($rootScope, $scope, $localStorage, AuthService){
  
      // Listening event apiRequested
      $scope.$on('event:apiRequested', function(e) {
  
          AuthService.token();
          // Restore the access_token in case it has changed
          access_token = $localStorage.getObject('access_token');
  
      });
  
  })
  </code></pre>
  
  <p><strong>UserController.js :</strong></p>
  
  <pre><code>$rootScope.$broadcast('event:apiRequested');
  
  // Get Users around
  return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
      return response;
  });
  </code></pre>
  
  <p>First thing i'm not sure about ... Does $http is processed if the event already executed entirely?</p>
  
  <p>So since i'm not sure, i'm thinking about adding a callback.</p>
  
  <p>Here the idea : </p>
  
  <pre><code>$rootScope.$broadcast('event:apiRequested', function(response){
  
      if(response){
  
          // Get Users around
          return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
              return response;
          });
  
      }
  
  });
  </code></pre>
  
  <p>Please let me know if it is possible to do that or should i use something else than event listener for that case.</p>