Angular JavaScript- Your First App- exercise- 9/11


#1

Click here for the exercise link

I do not understand this function plusOne bit in the controller together with the view:

  $scope.plusOne = function(index) { 

            $scope.products[index].likes += 1; 

             };

In the view:
<p class="likes" ng-click="plusOne($index)"> +{{product.likes}} </p>

Is "index" a reserved word? How plusOne function is working? If I change index with any other word it does not execute the function.


#2

$index is an Angular framework reference whereas the index in the controller is just a function parameter. We usually use $index within the scope of a ng-repeat directive. My recommendation is not to use $index when there is a filter applied to ng-repeat as the index can be messed up due to the filter. Ideal scenario, use $index only where it resides and not outside.


#3

Thanks so much! I will try to remember your recommendation.