Answer the question
In order to leave comments, you need to log in
How to correctly copy object properties in anjular.js?
The task is the following. There are two arrays: one with the original prices of manufacturers (manufacturers) and the other with prices that have been changed by datadilers. The code below shows a section of code that switches the rendering mode for dealer prices. And accordingly, I temporarily replace the price of the desired property in the working array ($scope.manufacturers), which is involved in my calculations. But for some reason, the property of the manufacturers array is also replaced: the price should be 10, but it becomes 500... although no operations are performed with this object. Where is the dog buried?
$scope.manufacturers = manufacturers;
$scope.datadilers = datadiler;
alert(manufacturers[$scope.order.manufacturer].series[$scope.order.series].euro[$scope.order.size].price); // БЫЛО 10
$scope.manufacturers[$scope.order.manufacturer].series[$scope.order.series].euro[$scope.order.size].price = $scope.datadilers[$scope.order.manufacturer].series[$scope.order.series].euro[$scope.order.size].price;
alert(manufacturers[$scope.order.manufacturer].series[$scope.order.series].euro[$scope.order.size].price); // СТАЛО ПОЧЕМУ-ТО 500, ХОТЯ НЕ ДОЛЖНО
Answer the question
In order to leave comments, you need to log in
To make it easier to understand why this happens, try to understand the concept of "assignment by reference". This is the basic concept of many languages. Without this knowledge, using JS is very difficult.
In order to solve your problem, you can do this:
$scope.manufacturers = angular.copy(manufacturers);
$scope.datadilers = angular.copy(datadiler);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question