title: Best practices for Clearing data in services on logout in AngularJs
body: <p>I have several services which uses a webservice and caches a lot of the results.By caching i mean storing in a variable on the service. When a user logs out the data should be cleared.
  The services looks like the following (simplified version):</p>
  
  <pre><code>class DataService   {
  
    private data;
  
    constructor($http)
    {
       $http.get(url).then((response) =&gt; 
         { 
            this.data = response.data;
         });
    }
  </code></pre>
  
  <p>}</p>
  
  <p>Which is typescript, but resolves to something like this:</p>
  
  <pre><code>var DataService = (function () {
      function DataService($http) {
          var _this = this;
          $http.get(url).then(function (response) {
              _this.data = response.data;
          });
      }
      return DataService;
  })();
  </code></pre>
  
  <p>I could clear the data using the answer in <a href="https://stackoverflow.com/questions/18509737/clear-scope-on-logout-in-angular-js">This Question</a> 
  Which is doing something like this: </p>
  
  <pre><code>$rootScope.on('logout',function(){
    this.data = [];
  });
  </code></pre>
  
  <p>However, this is a lot of code when we have multiple services and controllers. And we all know that this new guy will add some new data to a service and he forgets to add it to the logout sequence. It is simply a bad practice. </p>
  
  <p>Similarily, data are stored on $scope in various parts of the application and this must be cleared as well. 
  The scope is rather simple as constructors for the controllers are loaded on each page visits and will then override the data. </p>
  
  <p>One propossed solution is to make a refresh, however this gives horrible user experiences. </p>
  
  <p>One solution could be to make angular believe that the services have never been created or reload angular completely. </p>
  
  <p>What is the best way to do this? Is it bad to store data in variables on services?</p>