Reputation: 97
I am trying to update the data on a table over an interval of time.The code has got a routeProvider with an URL and controller like shown below. I am using init()
to get the data in controller. If i use init()
method inside $interval(), the table is updated with previous values. I mean if there are no new values and 10 records are there it becomes 20 records. How to update with new values alone.
Minimal app.js code
App.controller('PlantCtrl', [ '$scope', 'PlantDetails', '$interval'
, function($scope, PlantDetails, $interval,) {
$scope.availablePlantDetailsTemp = [];
$scope.availablePlantDetails = [];
init();
function init() {
$scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availablePlantDetails.push(item);
}
});
});
}
$interval(function() {
init();
}, 5000);
} ]);
Or if i use resolve inside routeProvider to do the query how can i call the method inside $interval()
Upvotes: 0
Views: 32
Reputation: 12113
You jsut need to clear array before adding new value to it. Array push
methods add new values to array hence you are facing the issue.
function init() {
$scope.availablePlantDetails = [];
$scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availablePlantDetails.push(item);
}
});
});
$interval(function(){
Init()
},1*2000)
Upvotes: 2