0

In angular 2 is necesary do copy or deep clone in data binding by reference and not exists a function angular.copy like in angular1, exists other coding pattern for do data binding by reference and not modified object in parent component when change object reference in child component, or the idea is to do a clone of object in parent component before to do the binding.

alehn96
  • 1,363
  • 1
  • 13
  • 23
  • 2
    That's a JS or TS feature and not related to Angular. In JS or TS if a value is passed primitives like string, number, boolean are passed by copy and objects (and arrays) are passed by reference. – Günter Zöchbauer Mar 22 '17 at 14:32
  • The question is why I have to do a clone when I pass a object by reference to child component and I don't want modified a object in parent component. The philosophy of angular 2 is to do a clone o deep clone? or exist other mechanism? – alehn96 Mar 22 '17 at 14:37
  • 2
    The philosophy of Angular2 is that it uses TypeScript (which compiles to JS) and which passes objects as reference. In TS/JS if you want a clone you need to make a clone. The default behavior is to pass a reference. There is no reason why Angular would deviate from the default behavior. – Günter Zöchbauer Mar 22 '17 at 14:39
  • 1
    Possible duplicate of [How to Deep clone in javascript](http://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript) – n00dl3 Mar 22 '17 at 14:40
  • . the only way for not modified objectParent when modified objectChild is to do a clone of objectParent and pass to component child the clone object – alehn96 Mar 22 '17 at 14:44
  • I am sorry for my english but the question is not about clone in javascript. It is about in angular 2 how pass complex object in data binding and don't affect the object in parent component when the object by reference in child component is modified – alehn96 Mar 22 '17 at 15:03
  • @n00dl3 gave you a dup link : you should check that out. Here is a link that might help you as well : http://stackoverflow.com/questions/35504310/deep-copy-an-array-in-angular-2-typescript/41637684#41637684 – Alex Beugnet Mar 22 '17 at 16:09

0 Answers0