我想要获取哪些数据已在数组中更改。我的用例是第一次所有数据从ajax中获取并显示在行内并每5秒使用$ http获取新数据。如果新数据与旧数据相同,我需要知道。如果是的话那么它的值发生了变化,所以我已经来更新后台一些颜色..如何知道数组中的哪些数据已被更改?
我已经尝试过
var app = angular.module('app', []);
app.controller('listingController', function($scope, $http, $timeout){
$scope.listings;
setInterval(function(){
$http.get('_includes/ajax.php?req=get').
success(function(data, status, headers, config) {
$scope.listings = data;
console.log(data);
}).
error(function(data, status, headers, config) {
// log error
});
}, 5000);
});
app.controller('RentalDateController', function($scope, $log){
console.log($scope.listings);
$scope.$watch('listings.Third_Column', function (Third_Column, oldvalue) {
//$log.info(Third_Column, $scope.listings.First_Column);
console.log('being watched oldValue:', oldvalue, 'newValue:', Third_Column);
}, true);
});
我的HTML是
<body ng-app="app">
<div ng-controller="listingController">
<table>
<tr>
<th>Testing</th>
<th>Pripse</th>
</tr>
<tr ng-repeat="row in listings" ng-controller="RentalDateController">
<input type="text" ng-model="row.Third_Column">
<input type="text" ng-model="row.First_Column">
<th>{{row.Third_Column}}</th>
<th>{{row.First_Column}}</th>
</tr>
</table>
</div>
</body>
我想我需要使用$ watch,但它不起作用。
矿山代码已经在更新DOM。我只需要,如果一些值已经在数组中改变,结果背景将是绿色的。假设第一次获取一个属性值是9,第二次获取后该值已更改为10,所以我需要显示属性背景为绿色 – Tariq 2014-12-06 07:33:36
我明白了,对不起,我在2点左右回答了这个问题。那么,你可以做的是先按照我所说的清理你的代码,然后像@Anubhav一样做,但首先创建一个名称和一个像{flag:'foo',isNew: 'true'}然后遍历数组并替换row.Third_column与对象,当你发现与旧的不一样的东西时,改变标志。现在你有一个自定义的$ scope.listing ...你可以把ng-class =“'make-green':row.Third_Column.isNew”并写入css approriately。 – gsalisi 2014-12-06 15:40:55
顺便说一句,你应该循环所有的值,并在收到数组时检查相等性。没有比这更快的方式,但这是它唯一的BigTheta(n)运行时。 – gsalisi 2014-12-06 15:43:23