title: Angularjs - $rootScope in directive link function
body: <p>I am asking this question because I am not quite clear on how to think of rootscope as a dependency passed to directives</p>
  
  <p>I have a directive that needs to display some information from $rootScope ...</p>
  
  <p>I thought I needed to pass the $rootScope to a directive but when I write a directive like this it seems to work.</p>
  
  <pre><code>.directive("myBar", function () {
   return {
      restrict: "E",
      transclude: true,
      replace: true,
      template:   '&lt;div&gt;' + 
                  '&lt;span ng-transclude&gt;&lt;/span&gt;' + 
                  '{{rsLabels.welcome}} {{rsUser.firstName}}!' + 
                  '&lt;/div&gt;'
  }
  })
  </code></pre>
  
  <p><strong>When do I need to do this?</strong></p>
  
  <pre><code>.directive("myBar", function ($rootScope) {
   return {
      restrict: "E",
      transclude: true,
      replace: true,
      template:   '&lt;div&gt;' + 
                  '&lt;span ng-transclude&gt;&lt;/span&gt;' + 
                  '{{rsLabels.welcome}} {{rsUser.firstName}}!' + 
                  '&lt;/div&gt;'
  }
  })
  </code></pre>
  
  <p><strong>Can I and HOW do I use rootScope if I need it in the link function of the directive</strong> - or should I do it in the controller of the directive?</p>
  
  <pre><code>.directive("myBar", function ($rootScope) {
   return {
      restrict: "E",
      transclude: true,
      replace: true,
      link: function (scope, element, attrs, rootScope) {
          rootScope.rsUser = { firstName: 'Joe' };
          rootScope.rsUser = { welcome: 'Welcome' };
      },
      template:   '&lt;div&gt;' + 
                  '&lt;span ng-transclude&gt;&lt;/span&gt;' + 
                  '{{rsLabels.welcome}} {{rsUser.firstName}}!' + 
                  '&lt;/div&gt;'
  }
  })
  </code></pre>
  
  <p>My rootScope data is defined in run function</p>
  
  <pre><code> .run(function ($rootScope) {
  
    $rootScope.rsLabels = { 
        welcome: 'Welcome'
    };
  
    $rootScope.rsUser = { 
       firstName: 'Joe'
    };
  });
  </code></pre>
  
  <p>Thank you!</p>