Preface
The index of arrays in JavaScript starts from 0, so when we take parity, we need to use !$even and !$odd to invert the boolean values of $even and $odd.
An example is given below:
Use $odd and $even to create a red and blue list
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <meta charset="utf-8"> <title>ng-repeatUsage</title> <link rel="stylesheet" href="../"> <style> .odd { background-color: blue; } .even { background-color: red; } </style> </head> <body> <h4>ng-repeatUsed to iterate through a collection or generate a template instance for each element in the collection。Each element in the collection They will be given their own templates and scopes。At the same time, some special attributes will be exposed in the scope of each template instance.。 </h4> <ul> <li>$index:The progress of traversal(0...length-1)。 </li> <li>$first:When the element is the first traversal value istrue。</li> <li>$middle:When the element is between the first and the next element, the value istrue。 </li> <li>$last:When the element is the next traversal value istrue。 </li> <li>$even:when$indexWhen the value is an even number, the value istrue。 </li> <li>$odd:when$indexWhen the value is an odd number, the value istrue。 </li> </ul> The following example shows how to use it$oddand$evenLet's make a red and blue list。remember,JavaScriptMedium array The index from0start,So we use!$evenand!$oddCome and$evenand$oddBoolean inversion。 <ul ng-controller="PeopleController"> <li ng-repeat="person in people" style="color: #fff;" ng-class="{even: !$even, odd: !$odd}"> {{ }} Living in {{ }} {{$index}} </li> </ul> <script src="../"></script> <script> ('app', []) .controller('PeopleController', ['$scope', function($scope) { $ = [ {name: 'Zhang San', city: 'Guangdong'}, {name: 'Li Si', city: 'Jiangxi'}, {name: 'Wang Wu', city: 'northeast'} ] }]) </script> </body> </html>
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.