Yonatan Naxon
Yonatan Naxon

Reputation: 639

angularjs ng-repeat duplicate items

I am using http request with angular js ng-repeat to repopulate a list.

it goes like this:

<div class="item" ng-repeat="item in itemlist">
    content goes here...
</div>

than, in the controller, I call a function with interval, that sets item variable to be the new list:

$app.communicate is a function that makes http request to the server

    initializeItemList = function() {
        $app.communicate('item','loadItemList',{user_id : localStorage.userId},function(data, status, headers, config) {
            $scope.recent_item = data.recent_item;
            $scope.itemlist= data.itemlist;
            }
        },function() {});       
    };

    initializeItemList();

    $interval(initializeItemList, 4000);

in practice, every interval run duplicates the itemlist.

I tried to add filter unique, but it won't help..

Upvotes: 0

Views: 210

Answers (1)

RahulB
RahulB

Reputation: 2110

Use track by

<div class="item" ng-repeat="item in itemlist track by $index">
    content goes here...
</div>

Upvotes: 4

Related Questions