0

I have an angular repeater bound to an array. When I push a new item, the UI updates as expected, but for subsequent pushes, the array updates but the UI doesn't change. I already tried scope.$apply() but it doesn't make any difference.

var app = angular.module('m', []);

app.controller('myCtrl', function ($scope) {
    $scope.maxValue = 6;
    $scope.values = [1,2,3,4,5,6];

    $scope.update = function (){
        $scope.maxValue++
        $scope.values.push($scope.maxValue);
        //$scope.$apply();
    }
});

Not sure what I'm doing wrong. Here's a small example that replicates the problem.

1 Answer 1

3

It's because you are pussing the same value again and again which creates duplicate elements, try using track by index

 <li data-ng-repeat="item in values track by $index"> 

working fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

ng-repeat doesn't allow duplicates by default? In my real app (not this example) I am actually pushing an object, which is different each time, so I don't understand why it would not work. Does it track by some default property like "id"?
no it does not allow duplcates, btw you are pussing a string not an object

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.