问题
When we pass a model
to 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
?
回答1:
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).
来源:https://stackoverflow.com/questions/40260158/angular2-pass-by-reference-to-interact-between-components