title: Angular run block, $rootScope.$on and $state
body: <p>I have a strange issue when I try create an eventhandler in the run block. It is a bit hard to explain the issue, partly because I don't understand what's happening but I will do my best. It's written in typescript.</p>
  <p>I have the following run-block:</p>
  <pre><code>angular.module(&quot;myapp&quot;)
    .run(($rootScope, StateChangeStartHandler) =&gt; {
      $rootScope.$on(&quot;$stateChangeStart&quot;, StateChangeStartHandler.handler);
    });
  </code></pre>
  <p><code>StateChangeStartHandler</code> is a simple angular service with a function called handler which is meant to handle the <code>$stateChangeStart </code>event. The handler service looks like this:</p>
  <pre><code>class StateChangeStartHandler {
  
    private AuthService;
    private $rootScope;
    private AUTH_EVENTS;
  
    constructor(AuthService, $rootScope, AUTH_EVENTS
               // Everything breaks if I comment in the following line
               //, $state
               ) {
      this.AuthService = AuthService;
      this.$rootScope = $rootScope;
      this.AUTH_EVENTS = AUTH_EVENTS;
    }
  
    handler(event, next) {
      if (!this.AuthService.isLoggedIn() &amp;&amp; next.name != &quot;root.login&quot;) {
        event.preventDefault();
        this.$rootScope.$broadcast(this.AUTH_EVENTS.notAuthenticated);
      }
    }
  }
  
  angular.module(&quot;myapp&quot;)
    .service(&quot;StateChangeStartHandler&quot;, StateChangeStartHandler);
  </code></pre>
  <p>I was implementing this using TDD. I wrote a test that verified if go was called on <code>$state</code>. When I then went into the class to implement it, I faced the issue. When I injected <code>$state</code> in the <code>StateChangeStartHandler</code>, suddenly all tests using <code>$rootScope.$apply()</code> said <code>AuthService.isLoggedIn</code> is not a function.</p>
  <p>I suspect that <code>$rootScope.$apply()</code> triggers a <code>$stateChangeStart</code>, and then something strange happens.</p>
  <p><strong>UPDATE 1:</strong><br />
  An example of a seemingly unrelated test that suddenly fails:</p>
  <pre><code>Chrome 45.0.2454 (Mac OS X 10.10.5) Authentication service login(email, password) should be defined FAILED
      TypeError: this.AuthService.isLoggedIn is not a function
          at StateChangeStartHandler.handler (/Users/blacksails/repos/avalonia/priv/static/js/app.js:305:31)
          at Scope.$broadcast (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:16311:28)
          at Object.transitionTo (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:50779:24)
          at Array.&lt;anonymous&gt; (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:49896:18)
          at Object.invoke (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:4584:17)
          at handleIfMatch (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:49386:28)
          at /Users/blacksails/repos/avalonia/priv/static/js/libraries.js:49441:18
          at check (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:49557:23)
          at update (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:49566:13)
          at Scope.$broadcast (/Users/blacksails/repos/avalonia/priv/static/js/libraries.js:16311:28)
  </code></pre>