ng-repeat not updating on update of array

后端 未结 5 952
我寻月下人不归
我寻月下人不归 2020-11-27 04:40

I\'m rendering data through an ng-repeat cycle. And i would like it to update as I update the array. From what i read this should happen automatically however this is not wo

5条回答
  •  余生分开走
    2020-11-27 05:03

    For those who have an array being set within scope (i.e. $scope.$apply() causes an error if called after a push), but it is still not updating correctly on the DOM, my workaround has been to call this function immediately after the push/slice/array update:

    function applyArray(container, key) {
        setTimeout(function() {
            var tempArray = container[key];
            container[key] = [];
            $scope.$apply();
            container[key] = tempArray;
            $scope.$apply();
        }, 0);
    }
    

    By passing in container and key, where container in this case would be $scope and key would be "dataDifferenceArray" (note the quotes).

    I have no idea why the DOM does not update correctly in some instances. It shows the correct number of elements, and it updates when a new element is added, it just doesn't update the child elements DOM correctly (i.e. the newly pushed element may take on the DOM view of the previous child). This could be due to how we redefine this to vm in Johnpapa's Angular Style Guide, but I'm not confident in that.

提交回复
热议问题