Reputation: 41
I want to share data between two direct related components (parent -child). I came across @ViewChild and @Output. First one will have more control and coding in parent component while later need coding in child component.
Which one should we chose over other?
Upvotes: 0
Views: 1439
Reputation: 2014
I think that you are looking for @Input and @Output.
With @Input you can send data from the parent component to a child component.
With @Output you can send data from child component to its parent as an event.
Upvotes: 0
Reputation: 30088
@ViewChild and @Output are two very different things, so it depends on exactly what you want to do.
@ViewChild gives the parent access to the child (component or element), while @Output emits an event to the parent when something in the child changes.
If you can do what you want with either of these, then I'd choose @Output, since it doesn't couple the parent and the child, it just presents a communication channel between them.
Upvotes: 1