0

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

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the tip. Could you also please tell me how to do it with resolve. I will update my resolve code also.
I have already added answer for that. See the second line of my answer
Yeah that is fine. I saw that. I want to know how to call the query from controller inside $interval()
Just call the init method from interval. Updated my answer. Check it
I asked if i use resolve inside routeProvider to do the query like i have soon above, how can i call that from $interval without using init method
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.