In the last article, we described the 2-way data binding with the ng-binding directive. In this article, I will introduce the ng-repeat directive.
Recall that a directive is a well-known attribute defined by AngularJS or your AngularJS application that is applied to an HTML element to provide some functionality.
The ng-repeat directive acts in a similar way to foreach loops or other constructs in a programming language. We point ng-repeat to an array of objects and Angular will loop through this array, repeating the HTML contained in the attributed element once for each element in the array.
In the following example, our controller creates an array of "customers" and adds it to the $scope directive, making the customers stateful and available to the HTML. Each customer has a firstName, lastName, and salary property.
In our HTML, we can loop through every customer in the array by applying the ng-repeat directive to a
Because the array contains 3 customers, the
Here is the markup for the entire table.
The beauty of this is that we don't need to know in advance how many customers we have or how many rows we need. Angular figures it out for us.
The ng-repeat directive allows us the flexibility of repeating HTML markup for every element in an array.