title: Accessing scope variable in function in AngularJS
body: <p>I'm still learning AngularJS and I'm trying to (for example) switch the names on a regular interval. I've got most of the basic idea down but I'm running into problems with how Angular handles how information is shared I've tried this:</p>
  
  <p><a href="http://jsfiddle.net/nwz5krw4/1/" rel="nofollow noreferrer">http://jsfiddle.net/nwz5krw4/1/</a>
  The core of the fiddle is this (where names is an array of names):</p>
  
  <pre><code>this.changeName = function($scope,names)
      {
          this.nameIndex++;
          if ($scope.$parent.nameIndex &gt; (names.size - 1))
          {
              $scope.$parent.nameIndex = names[0];
          }
          else
          {
              $scope.$parent.myName = names[$scope.$parent.nameIndex];
          }
  
      };
  </code></pre>
  
  <p>Oddly, that will load in the browser and give me the first name, then simply refuse to change Reading the console reveals that t. Another path I've taken based on <a href="https://stackoverflow.com/questions/18880737/how-do-i-use-rootscope-in-angular-to-store-variables">How do I use $rootScope in Angular to store variables?</a> is to create this:</p>
  
  <pre><code>app.run(function($rootScope){
  
  });
  </code></pre>
  
  <p>And place my array of names in there as $rootScope.names. Obviously $rootScope is to be avoided where possible, but right now I'm just going for functionality. However, this, too, did not work. Passing $scope to the method resulted in $scope.names or $rootScope.names being undefined. I'm not yet "thinking in Angular" and need some mental translation to make the leap.</p>