AngularJS Basic ng class odd Instruction Example

  • 2021-07-07 06:03:01
  • OfStack

AngularJS ng-class-odd Directive

AngularJS instance

Set class= "striped" for odd rows of the table:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<style>
.striped {
  color:white;
  background-color:black;
}
</style>
</head>
<body ng-app="myApp">

<table ng-controller="myCtrl">
<tr ng-repeat="x in records" ng-class-odd="'striped'">
 <td>{{x.Name}}</td>
 <td>{{x.Country}}</td>
</tr>
</table>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
 $scope.records = [
  {
   "Name" : "Alfreds Futterkiste",
   "Country" : "Germany"
  },
  {
   "Name" : "Berglunds snabbk",
   "Country" : "Sweden"
  },
  {
   "Name" : "Centro comercial Moctezuma",
   "Country" : "Mexico"
  },
  {
   "Name" : "Ernst Handel",
   "Country" : "Austria"
  }
 ]
});
</script>

</body>
</html>

Run results:

Alfreds Futterkiste Germany
Berglunds snabbk Sweden
Centro comercial Moctezuma Mexico
Ernst Handel Austria

Definition and usage

The ng-class-odd directive is used to dynamically bind one or more CSS classes for an HTML element, but only for odd rows.

The ng-class-odd instruction needs to be used with the ng-repeat instruction.

The ng-class-odd directive is recommended for style rendering of tables, but all HTML elements are supported.

Grammar

< element ng-class-odd="expression" > < /element >

All HTML elements are supported.

Parameter value

描述
expression 表达指定1个或多个 CSS 类。

The above is the collation of AngularJS data, which is referred to by friends in need.


Related articles: