我正在尝试比较两个变量,但是我做错了什么,但我没有想要的结果。我在从$ scope到数组中输入数据时遇到问题。
我要补充一点,我是一个超级外行,我正在学习。
你可以看看这个吗?
继续主题:Difference between two adjacent numbers in array和Finding difference between consecutive numbers in an array in javascript
我的代码:
<div ng-app="myApp" ng-controller="customersCtrl">
<div class="container">
<div class="row">
<div class="table">
<table class="table" id="analyticsTable" ng-repeat="(key, value) in names | groupBy: 'view_id'">
<thead>
<tr>
<td><strong>{{key}}</strong></td>
<th ng-repeat="item in value | where:{year: 2017}">{{ item.month }}</th>
</tr>
</thead>
<tr>
<td>2017</td>
<td ng-repeat="item in value | where:{year: 2017}">{{ item.visit }}</td>
</tr>
<tr>
<td>2018</td>
<td ng-repeat="item in value | where:{year: 2018}">{{ item.visit }}
<span class="visited">{{ item.visit | myFormat }}</span>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
控制器代码:
app.controller('customersCtrl', function($scope, $http) {
$http.get("ga_data.php")
.then(function(response) {
$scope.names = response.data.records;
});
});
app.filter('myFormat', function() {
return function(input, scope) {
var number = "";
checkVisitsDiff(x);
function checkVisitsDiff(x) {
if (!x || x.length < 2) {
throw "Bad input!";
}
for (var i = 1; i < x.length; i++) {
var currentElement = x[i];
var previousElement = x[i - 1];
number = (currentElement - previousElement);
}
}
return number;
}
});