Angular2: Pass by reference to interact between components

Sumit Agarwal picture Sumit Agarwal · Oct 26, 2016 · Viewed 21.5k times · Source

When we pass a modelto the child component and it modifies it, the values are just reflected in the child components' local variable and not available to the parent. Can we pass values by reference from parent to child. So the changes are visible there as well.

I have implemented the same using an observable at the service layer. But can't we pass by reference through @Input?

Answer

Günter Zöchbauer picture Günter Zöchbauer · Oct 26, 2016

Primitive values (string, num, boolean, object references) are passed by value (copied), objects and arrays are passed by reference (both components get a reference to the same object instance).

Just wrap your primitive values in objects and changes will be reflected on both sides.

Angular2 change detection won't detect changes to values in arrays or object properties (except when binding expressions address them).