Communicating and sharing data between components
We already used the simplest way to communicate and share data between components: the Input
and Output
decorators. Properties decorated with the Input
decorator initialize a component by passing through data, and the Output
decorator can be used to assign event listeners that will receive data out of the component. This approach can be observed with the components found in the Example2
folder from the source code for this chapter.
Referencing child components from a parent component
We can bypass the declarative binding to component properties and events using a template reference variable or by injecting the target component into the parent component via the ViewChild
and ViewChildren
property decorators. In both the scenarios, we get a reference to the target component and can assign its properties or invoke its methods programmatically. To demonstrate these capabilities in action, we will slightly alter the ChildComponent
class from Example2...