2014-12-06 119 views
0

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

回答

1

你有角的双向数据绑定,它会自动更新您的NG-重复时模型的变化。我建议如下 1)先移除RentalDate控制器 2)使用$超时,并在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

+0

矿山代码已经在更新DOM。我只需要,如果一些值已经在数组中改变,结果​​背景将是绿色的。假设第一次获取一个属性值是9,第二次获取后该值已更改为10,所以我需要显示​​属性背景为绿色 – Tariq 2014-12-06 07:33:36

+0

我明白了,对不起,我在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

+0

顺便说一句,你应该循环所有的值,并在收到数组时检查相等性。没有比这更快的方式,但这是它唯一的BigTheta(n)运行时。 – gsalisi 2014-12-06 15:43:23

0

尝试这样做:

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

    }); 
+0

它总是显示'新数据可用!'我仍然没有得到改变的价值。我想要的是这个。一旦数值发生变化,​​就会有几秒钟的绿色背景。 – Tariq 2014-12-06 07:13:48

相关问题