user3626386 user3626386 - 4 months ago 8
AngularJS Question

Angular changes values in all arrays

my code is here:
https://jsfiddle.net/enktfprq/4/
i think problem is here:

angular.copy($scope.items[$index], $scope.editedItem);

but i define which array is to be changed

please add 2 time pizza margharitta.
In the right column is json of array $scope.items
so, when im adding 2 times the same item, and trying to change price one of it(and then click green button) angular is changing price in all arrays.
I found something similar http://jsfiddle.net/p03o7rab/

So, what am I doing wrong, and why angular is changing value price ('cena') in all arrays

Sorry for my english

Answer

Adjust the body of your $scope.dodajPozycjeDoParagonu() function like this:

  $scope.dodajPozycjeDoParagonu = function(index, index2) {
    var item = {};
    angular.copy($scope.produkty[index].produkty[index2], item);
    $scope.items.push(item); ...

This should do the trick. Hope it helps.

Comments