Jacob Schulke Jacob Schulke - 2 years ago 58
AngularJS Question

Angular repeaters element in collection

I am trying to learn angular. I am having a hard time understanding part of this: http://www.learn-angular.org/#!/lessons/repeaters

In that example we're looping through a collection and displaying each element. Easy enough.

What I don't understand is: the collection is named "products" and each element is referred to as "product" no where in the JS code is the word product. How does angular know to call the element by "product"? Does a collection have to be plural and an element singular in name?

Answer Source

The identifier is assigned in the view (aka, the HTML):

<tr ng-repeat="product in products">

ng-repeat takes an expression that defines the identifier it will assign elements in the collection. So, in this case, you bind each item in products to product. This is then accessible in the scope, so any child element of the tr can access product on the scope as $scope.product.

You can name the identifier anything you want. product in products is equally as valid as bananas in apple, so long as your collection is on $scope.apple and you use the identifier bananas to refer to the item in each collection.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download