Communicating between components can be a challenging concept to grasp. Feat not, I will show you how to effectively communicate between your components using a simple "service" design pattern.
Communicating between components can be a challenging concept to grasp. Fear not, I will show you how to effectively communicate between your components using a simple "service" design pattern.
At a high level the service class will hold our variables. When we want to access, or modify, these variables we will simply use dependency injection and "inject" our service class into the component class(es) that need to access this information.
A service is nothing more than a class that contains reusable logic that you want to share between other services and components.
This is what the service class looks like based on the needs for this demo. You can add ass many properties as you want!
The Component
Our component will use dependency injection to inject our MyServiceService class instance and because we're making it public we can access it's properties and methods from within our template.
The Component Class:
The Component Template:
As you can see by using [formControl]="myServiceService.formControl" we are binding the <input> attribute [formControl] to the FormControl instance from our service class.