我使用angular.js作为我的font-end,node.js作为服务器端,PostgreSQL作为数据库.
现在,我在DB中有一些值列表.
数据库:
控制器代码:
我在控制台中得到了以下输出.
console.log($scope.items);
$scope.items = [
{
salary_head_name : 'BASIC',
salary_head_value : 15000,
salary_head_type : 'E'
}, {
salary_head_name : 'HRA',
salary_head_value : 7500,
salary_head_type : 'E'
},{
salary_head_name : 'Conveyance',
salary_head_value : 1600,
salary_head_type : 'E'
},{
salary_head_name : 'Med. Allow',
salary_head_value : 1800,
salary_head_type : 'E'
},{
salary_head_name : 'PF',
salary_head_value : 1800,
salary_head_type : 'D'
}
];
注意:为了便于阅读,我在上面的数组中只打印了几条记录(手动编辑),实际上它会打印所有记录.
UI中的预期输出:
此外,我还想在上表的总计字段中打印收入和扣减的总和.
最佳答案 使用
ng-repeat,基本想法请看下面的代码:
(function(angular) {
'use strict';
angular.module('ngRepeat', ['ngAnimate']).controller('repeatController', function($scope) {
$scope.items = [{
salary_head_name : 'BASIC',
salary_head_value : 15000,
salary_head_type : 'E'
}, {
salary_head_name : 'HRA',
salary_head_value : 7500,
salary_head_type : 'E'
},{
salary_head_name : 'Conveyance',
salary_head_value : 1600,
salary_head_type : 'E'
},{
salary_head_name : 'Med. Allow',
salary_head_value : 1800,
salary_head_type : 'E'
},{
salary_head_name : 'PF',
salary_head_value : 1800,
salary_head_type : 'D'
}];
});
})(window.angular);
table {
border: 1px solid #666;
width: 100%;
}
th {
background: #f8f8f8;
font-weight: bold;
padding: 2px;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-ngRepeat-production</title>
<link href="animations.css" rel="stylesheet" type="text/css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="ngRepeat">
<div ng-controller="repeatController">
<table>
<tr>
<th>Earnings</th>
<th>Amount</th>
<th>Type</th>
</tr>
<tr ng-repeat="item in items">
<td>{{item.salary_head_name}}</td>
<td>{{item.salary_head_value}}</td>
<td>{{item.salary_head_type}}</td>
</tr>
</table>
</div>
</body>
</html>