Having a parent-child relationship between components allows for easy dependency injection between them.
If you have a setup like the one below:
@Component( {
selector:"app-parent",
template:" <app-child> </app-child>"
} )
export class ParentComp { ...}
You can inject the parent component inside the child component using dependency injection.
@Component({
selector:"app-child",
template:"I am child"
})
export class ChildComponent{
constructor(private parentComp:ParentComponent){
}
}
The Angular DI system will recognize that you need access to the parent component of the child and handle the injection for you.
If you need to inject a component that does not have a parent-child relationship, such as wanting to inject a sidenav component into a table component outside of the sidenav's structure, it is technically possible but not recommended. In such cases, creating a shared service to manage state between these components is a better approach.