简体   繁体   English

如何知道数组中的哪些数据已更改?

[英]How to know which data has been changed in an array?

I'm trying to get which data has been changed in array. 我正在尝试获取数组中已更改的数据。 my use case is First time all data fetch from ajax and show within row and fetch new data using $http every 5 second. 我的用例是第一次,所有数据都从ajax获取并在行中显示,并每5秒使用$ http获取新数据。 I need to know that if the new data is same as old one. 我需要知道新数据是否与旧数据相同。 If yes then which value has changed so I've to update that background to some color.. 如果是,则哪个值已更改,所以我必须将该背景更新为某种颜色。

What I've already tried 我已经尝试过的

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);
    });

My html is 我的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>

I think I need to use $watch but it's not working. 我想我需要使用$ watch,但是它不起作用。

You have the angular two-way data binding so it should automatically update your ng-repeat when the model changes. 您具有角度双向数据绑定,因此当模型更改时,它应该自动更新ng-repeat。

I suggest the following 1) Remove RentalDate controller first. 我建议以下1)首先删除RentalDate控制器。 2) Use $timeout, and on success of http use this 2)使用$ timeout,并在成功使用http时使用this

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

If that doesn't still automatically update, put the array in an object. 如果仍然不能自动更新,请将数组放入对象中。

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

This will work because of this. 因此,这将起作用。 Read up. 阅读。 :D :d

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

Try doing this: 尝试这样做:

var app = angular.module('app', []);

    app.controller('listingController', function($scope, $http, $timeout){

        $scope.listings;
        var previousListing = [];
        var newData;

        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
            });

            if( previousListing.length == $scope.listings.length )
            {
                console.log('No new data available.');
            }
            else
            {
                // length of the arrays are different, hence new data must be available
                console.log('New data available!');
                newData = $scope.listings.splice( 0, ($scope.listings.length - previousListing.length) ); // returns new data in the form of an array
                console.log(newData);
                previousListing = $scope.listings; // reset previous data variable

            }

        }, 5000);

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM