title: Angular spinning directive gets instantiated after controller load
body: <p>I have a controller. Here is the relevant part of the constructor function (what is the correct term for this function?):</p>
  
  <pre><code>activate();
  
          function activate() {
              $scope.$broadcast('ctrlLoadingStarted');
                  var promises = [getUsers()];
                  return $q.all(promises).then(function (eventArgs) {
                      $scope.$broadcast('ctrlLoadingFinished');
              });
          }
  </code></pre>
  
  <p>So the activate function is basically a generic function that takes in an array of data getting functions. It broadcasts on start and broadcasts on finish.</p>
  
  <p>Then I have a directive nested in the controllers scope, here is the relevant part of the directive:</p>
  
  <pre><code>function link(scope, element, attrs) {
              scope.$on('ctrlLoadingStarted', function (event, args) {
                  scope.spinnerOn = true;
              });
              scope.$on('ctrlLoadingFinished', function (event, args) {
                  scope.spinnerOn = false;
              });
          }
  </code></pre>
  
  <p>As you can see, it is simply listening to the start and finish events and turning a spinner on and off.</p>
  
  <p>The issue is that the controller seems to be instantiating before the directive. This results in the broadcast going off and not turning the spinner on. </p>
  
  <p>I tried to simply put <code>scope.spinnerOn = true;</code> in the first line of the directive (this way, no matter when the controller instantiates relative to the directive instantiation, the spinner will always go on). However, the problem I ran into there was that the controller's second broadcast would go out before the directive was instantiated as well causing an endless spinner.</p>
  
  <p>I just want to ensure that if the controller instantiates before the directive but then the data-getting takes a while that the spinner will spin. </p>
  
  <p>All advice is appreciated, thanks in advance.</p>