欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AngularJS基础知识笔记之表格

程序员文章站 2022-06-26 19:32:40
表格数据本质上通常是重复的。ng-repeat指令,可以用来方便地绘制表格。下面的示例说明使用ng-repeat指令来绘制表格。 ...

表格数据本质上通常是重复的。ng-repeat指令,可以用来方便地绘制表格。下面的示例说明使用ng-repeat指令来绘制表格。

<table>
  <tr>
   <th>name</th>
   <th>marks</th>
  </tr>
  <tr ng-repeat="subject in student.subjects">
   <td>{{ subject.name }}</td>
   <td>{{ subject.marks }}</td>
  </tr>
</table>

表格可以使用css样式设置样式,如下:

<style>
table, th , td {
  border: 1px solid grey;
  border-collapse: collapse;
  padding: 5px;
}
table tr:nth-child(odd) {
  background-color: #f2f2f2;
}
table tr:nth-child(even) {
  background-color: #ffffff;
}
</style>

例子
下面的例子将展示上述所有指令。

testangularjs.html

<html>
<head>
<title>angular js table</title>
<style>
table, th , td {
 border: 1px solid grey;
 border-collapse: collapse;
 padding: 5px;
}
table tr:nth-child(odd) {
 background-color: #f2f2f2;
}
table tr:nth-child(even) {
 background-color: #ffffff;
}
</style>
</head>
<body>
<h2>angularjs sample application</h2>
<div ng-app="" ng-controller="studentcontroller">
<table border="0">
<tr><td>enter first name:</td><td><input type="text" ng-model="student.firstname"></td></tr>
<tr><td>enter last name: </td><td><input type="text" ng-model="student.lastname"></td></tr>
<tr><td>name: </td><td>{{student.fullname()}}</td></tr>
<tr><td>subject:</td><td>
<table>
  <tr>
   <th>name</th>
   <th>marks</th>
  </tr>
  <tr ng-repeat="subject in student.subjects">
   <td>{{ subject.name }}</td>
   <td>{{ subject.marks }}</td>
  </tr>
</table>
</td></tr>
</table>
</div>
<script>
function studentcontroller($scope) {
  $scope.student = {
   firstname: "mahesh",
   lastname: "parashar",
   fees:500,
   subjects:[
     {name:'physics',marks:70},
     {name:'chemistry',marks:80},
     {name:'math',marks:65},
  {name:'english',marks:75},
  {name:'hindi',marks:67}
   ],
   fullname: function() {
     var studentobject;
     studentobject = $scope.student;
     return studentobject.firstname + " " + studentobject.lastname;
   }
  };
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>

以上所述就是本文的全部内容了,希望大家能够喜欢。