Bernardo Pacheco
Bernardo Pacheco

Reputation: 1445

How to pass a parent property by reference instead of by value through an @Input decorator to a child component?

If a parent component pass a property to a child component through @Input decorator, it's passed by value, not by reference. If inside the child component I want to change the parent's property passed by @Input, I have to emit the new value through an @Output decorator, which we'll be captured by the parent component and assigned to the parent's property.

Is it possible to pass a property through an @Input decorator by reference in Angular 2?

Regards,

Bernardo

Upvotes: 3

Views: 1886

Answers (1)

TGH
TGH

Reputation: 39278

Just pass an object to the @Input. That will pass it as a reference.

If you change one of the properties on the object it will update referenced data.

example:

{name:'Joe'}

If the child updates the name property it will be seen by the parent.

Upvotes: 2

Related Questions