0

I want to edit an array of objects, which are tied to the view by an ng-repeat. How can I avoid updating the scope until the complete array has been modified?

Here is a simple example:

I would like to update every obj in array and ONLY then update the $scope. Since the array contains objects I cannot simply copy them over in another variable.

<div ng-click="updateAll()" ng-repeat="obj in array">obj.value</div>

angular.module("test", []).controller('Ctrl', function($scope) {
    $scope.array = [{val: 1}, {val: 2}, {val: 3});

    $scope.updateAll = function() {
        for (var i = $scope.array.length - 1; i >= 0; i--) {
            $scope.array[i].val2 = i;
        };
    };
});
Sergej
  • 376
  • 4
  • 15

1 Answers1

0

I guess your only possibility is to edit another array and set it to your original array as soon as all updates are done.

kabaehr
  • 1,040
  • 11
  • 18
  • My array contains objects. I would hate to deep clone it (might loose data, like getters...) – Sergej Apr 09 '16 at 17:12
  • why do you want this behavior? Do you show a list of the items and provide a possibility to update them, one after another and don´t want to have the items saved before the user clicked save or something similiar? – kabaehr Apr 09 '16 at 17:16
  • It is simply a performance issue. I do not want to update the $scope 150 times for all my objects if one update would suffice... – Sergej Apr 09 '16 at 17:18