AngularJS | ng-class-even指令用法示例

2021年3月11日17:05:02 发表评论 51 次浏览

ng-class-even指令在AngularJS中, 用于在HTML元素的所有均匀外观上指定CSS类。它用于在每个偶数HTML元素上动态绑定类。如果ng-class-even指令中的表达式返回true, 则仅添加该类, 否则不添加。要使ng-class-even指令有效, 必须使用ng-repeat指令。所有HTML元素都支持它。

语法如下:

<element ng-class-even="expression"> Contents... </element>

例子:本示例使用ng-class-even指令选择偶数元素并应用CSS属性。

<!DOCTYPE html>
< html >
      
< head >
     < title >ng-class-even Directive</ title >
      
     < script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js" >
     </ script >
      
     < style type = "text/css" >
         .index {
             color: white;
             background-color: green;
         }
     </ style >
</ head >
  
< body ng-app = "app" style = "padding:20px" >
      
     < h1 style = "color:green" >lsbin</ h1 >
     < h2 >ng-class-even Directive</ h2 >
  
     < div ng-controller = "geek" >
         < table >
             < thead >
             < th >sorting techniques:</ th >
             < tr ng-repeat = "i in sort" >
                 < td ng-class-even = "'index'" >
                     {{i.name}}
                 </ td >
             </ tr >
             </ thead >
         </ table >
     </ div >
      
     < script >
         var app = angular.module("app", []);
         app.controller('geek', ['$scope', function ($scope) {
             $scope.sort = [
                 { name: "Merge sort" }, { name: "Quick sort" }, { name: "Insertion sort" }, { name: "Bubble sort" }
             ];     
         }]);
     </ script >
</ body >
  
</ html >

输出如下:

ngclasseven

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: