If you are creating any web based application using AngularJS, and there are chances that you have to show repeated data based on what you are getting from API’s. For this ng-repeat is very important and useful directive for repeating UI elements.
So in this will see how we can use ng-repeat along with it’s different possible options.
Following properties will be discussed in this tutorial,
Let’s start with basic ng-repeat. We will use table to show iteration.
So here is our controller in which we call our API and bind data to UI elements.
Simple ng-repeat :
$scope.details = ;
$scope.details = data;
So in above code we simply calling mock json file and then passing that data to $scope.details.
Here is the html file in which will use $scope.details data to show entries.
So in details we have all the data and that will pass one by one to item and in that item we are showing id, first_name etc.
ng-repeat with key & value :
We can also use key and value in ng-repeat. So controller code is same as above. we just need to change in html code,
In above code you can see that we are accessing key and value to show data.
$odd in ng-class with ng-repeat :
In table if we need to change anything for odd rows then we can use $odd to filter out odd number of rows. and based on that we can change css or any other operations.
in this example we have changed the odd row with css so that you can see green color for odd number of rows.
$even in ng-class with ng-repeat :
In table if we need to change anything for even rows then we can use $even to filter out even number of rows and based on that we can change css or any other operations.
in this example we have changed the even row with class to danger using the ng-class directive so that you can see red color for even number rows.
$index in ng-class with ng-repeat :
In table if we need to change anything for row based on index then we can use $index.
In this example we have adding ‘text-primary’ class based on index matched on 3.
$first in ng-class with ng-repeat :
In table if we need to change anything for first row then we can use $first.
in this example we have adding ‘text-danger’ class for first row.
$last in ng-class with ng-repeat :
In table if we need to change anything for last row then we can use $last.
in this example we have adding ‘text-info’ class for last row.
$middle in ng-class with ng-repeat :
In table if we need to change anything for middle rows except first and last then we can use $middle.
in this example we have adding ‘danger’ class for middle rows.
In this article we have learned the use of ng-repeat along with various options in details.
Meet Mukul, a passionate visionary and a dedicated 3D printing enthusiast. With an insatiable curiosity for technology and a flair for creativity, Mukul has discovered a world where innovation knows no bounds. Armed with a deep understanding of 3D printing and its endless possibilities, he has become a true pioneer in the field, constantly pushing the boundaries of what can be achieved with this remarkable technology.