Update component/Angular Object 6

Asked

Viewed 1,071 times

-1

I have 3 components that exchange an Object among themselves, as I do for when updating one another also upgrade alone?

Example: I have components 1, 2 and 3, 1 calls 2 passing an Object and 2 calls 3 passing the same Object, all this in the creation of the components. I have to update component 3 when Object in component 1 changes.

  • You have to notify the parent event, and so on that an event took place in the child component. You need this directive: https://angular.io/api/core/EventEmitter. Demo here:https://stackblitz.com/ we’ll know the problem better.

1 answer

1

To do this you must use @Input, passing from parent to child the value you want.

In this example you have given, the properties of Component 2 and 3 must have the @Input annotation, for parents to 'insert' the value in the children. Component inputa no 2, o 2 inputa no 3, so only 2 and 3 need @Input. And so on.

To send value from children to parents, you must use @Output.

The details of how to use @Input and @Output on the Angular website: https://angular.io/guide/component-interaction

I made an example for you to see working: https://stackblitz.com/edit/angular-pufge7

Browser other questions tagged

You are not signed in. Login or sign up in order to post.