Let's say I have a component that takes in a Foo
instance and displays a form to edit it.
export class ChildComponent implements OnInit {
@Input() foo : Foo;
@Output() onChange : EventEmitter<Foo> = new EvenEmitter<Foo>();
constructor() {
}
ngOnInit() {
}
}
I nest that ChildComponent
inside a ParentComponent
.
<div id="parent">
<app-child-component [foo]="parentFoo"></app-child-component>
</div>
Now even though I used 1-way binding here, since foo
is an object and thereby passed by reference, any changes made to it in ChildComponent
are also reflected in ParentComponent
.
How can I prevent that? Is there a way to pass by value? Any best practices?