Good afternoon, I am an amateur in TS and Angular2, and I ask for help from more knowledgeable people. I decided to rewrite the old interface for the store to TS and Angular

http://pastebin.com/imJmEhhn - listing

Actually the question: In the CartFront class (line 75 of the listing) I get data from the rendered _searchres json array and send them as a parameter to AddCartItem (line 86 of the listing) The function CartFront private array of the CartFront class. When I try to change the price_cena field in the same function, the price_cena field also price_cena in my item.price_cena . As a result, the _searchservice data is _searchservice .

It is likely that I do not understand some elementary things, but to be honest, I had no such problems with knockoutjs.

  • Make a minimal reproducible example in the planer. Although it is very likely that you do not really understand how the reference types work, and the angular and type scripts have nothing to do with it. - Qwertiy
  • You would learn the basics of Javascript, eh? Namely, how objects behave. Why do I need to clone objects, etc. - zhenyab

1 answer 1

The point here is not TypeScript, but js. You pass in an AddItem (result) - this is an object. Objects are passed by reference. Ie you do not get a copy of the object, but the same object and change it, the _searchres object changes accordingly. If you need to copy an object you can do this:

 var a = JSON.parse(JSON.stringfy(b)) 

the object b will be converted to a string, and then parsed into json and the entire object will be copied, not the link.