on enter controller call function angularJS
<p>The problem is that i need to call a function every single time that a route change to a specific state, lets say i have chatController and i have to fire a() every second but if i exit the controller i have to stop a() and when i'm back to chatController i have to restart a()</p>

<p>My code: </p>

<pre><code>$scope.stop = $interval(yourOperation, 1000);

  var dereg = $rootScope.$on('$locationChangeSuccess', function() {   
    $interval.cancel($scope.stop);
    dereg();
  });


  function yourOperation() {
    console.log('$location', $location.$$url)    
    console.log('test');
  }
</code></pre>

<p>Works fine executing every single and stops when the controller change, but it doesn't work anymore if i go back, i tried with ng-init() function but only fires the first time that the controller start, i need it always when i'm on a specifict controller.</p>