Member-only story

Set your parents free with Angular’s dependency injection

Yoko Ishioka
Level Up Coding
Published in
3 min readNov 4, 2021
planes flying to goal
Source: Freepik

If you’re using a modular, component-driven architecture, one of the greatest challenges you will face is how to enable communication between parent and child components/directives. On the one hand, you want each component/directive to be independently focused on their purpose. On the other hand, you need them to work together as though they were built as a cohesive unit.

Luckily for us, Angular provides everything you need for any type of situation!

If your parent view contains the children, a common approach is to use the @Input() and @Output() decorators. To communicate with the parent, you can emit custom events through your children. To communicate with the children, you can pass properties that are bound to your parent.

You can also use @ViewChild/@ViewChildren and @ContentChild/@ContentChildren to break down the parent/child walls. (I will discuss these powerful decorators in future articles.)

If your parent view doesn’t contain the children directly or perhaps there is no relationship at all, you can use observables in a service to communicate data. That way, it doesn’t matter what the actual relationship is because anyone who injects the service can update and subscribe to the observables.

Why use dependency injection to find…

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Written by Yoko Ishioka

the web is a digital playground, only bound by imagination. there’s something new to learn every day. https://cloudengineering.studio

No responses yet

Write a response