因此,我试图在每个日期的两次折扣之间取得差异(得到那个),然后将所有百分比加起来并获得平均值,但我无法弄清楚如何。数据来自JSON。有任何想法吗?非常感谢帮助。
这是html:
<div ng-controller="MainController as main">
<section class="view">
<h1>Stuff</h1>
<ul class="stuff">
<li ng-repeat="item in main.logs">
<p>{{$index + 1}}</p>
<p>{{ item.date }}</p>
<p>{{ main.logs.calc(item.start, item.loggedIn) }} %</p>
</li>
</ul>
</section>
</div>
app.js文件
angular.module('myApp', [
'MainController'
]);
控制器,到目前为止我所拥有的:
angular.module('myApp', [])
.controller('MainController', ['$http', function($http){
var checklog = this;
checklog.logs = [];
$http.get('./login.json').success(function(data){
checklog.logs = data;
checklog.logs.calc = function(d1, d2) {
checklog.logs.percent = [];
var d1 = new Date(d1).getTime();
var d2 = new Date(d2).getTime();
var diff = ( ((d2 - d1) / 60000) / 60 ) * 100 ;
checklog.logs.percent = 100 - Math.floor(diff);
return checklog.logs.percent;
}
});
}]);
JSON是这样的:
[{
"date" : "2015-09-15",
"start" : "2015-09-15 17:00:00+10",
"loggedIn" : "2015-09-15 17:06:27+10",
}]