簡體   English   中英

如何知道數組中的哪些數據已更改?

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

我正在嘗試獲取數組中已更改的數據。 我的用例是第一次,所有數據都從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,但是它不起作用。

您具有角度雙向數據綁定,因此當模型更改時,它應該自動更新ng-repeat。

我建議以下1)首先刪除RentalDate控制器。 2)使用$ timeout,並在成功使用http時使用this

$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

嘗試這樣做:

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