gpt4 book ai didi

javascript - 如何知道数组中哪些数据被更改了?

转载 作者:行者123 更新时间:2023-12-03 11:16:20 25 4
gpt4 key购买 nike

我正在尝试获取数组中哪些数据已更改。我的用例是第一次从 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 但它不起作用。

最佳答案

您拥有 Angular 双向数据绑定(bind),因此当模型更改时它应该自动更新您的 ng-repeat。

我建议如下1)首先删除RentalDate Controller 。2)使用$timeout,并在http成功时使用此

$scope.$apply(function(){
$scope.listing = data;
});

如果仍然没有自动更新,请将数组放入对象中。

$scope.data = {}
$scope.data.listings = putArrayHere();

正因为如此,这才会起作用。读起来。 :D

https://github.com/angular/angular.js/wiki/Understanding-Scopes#javascript-prototypal-inheritance

关于javascript - 如何知道数组中哪些数据被更改了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27328795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com