Angular Inter-Component Communication
Tags: angular, typescript
Published: 2020-02-02 07:10:28

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.


Live Demo https://ng-byexamples-component-comms.herokuapp.com/

# The Service 

A service is nothing more than a class that contains reusable logic that you want to share between other services and components.

```typescript
import { Injectable }  from '@angular/core';
import { FormControl } from '@angular/forms';

@Injectable({
    providedIn: 'root'
})
export class MyServiceService {

    /**
     * Holds the current page name.
     *
     * @type {string}
     */
    public currentPage: string;

    /**
     * Holds the value of the <input>'s.
     *
     * @type {number}
     */
    public someNumber: number = 0;

    /**
     * FormControl to hold the value of the <input> on each components page.
     * @type {FormControl}
     */
    public formControl: FormControl = new FormControl('');

    public constructor() {

        //
        // Subscribe to the formControl's changes and update the service value.
        //
        this.formControl.valueChanges.subscribe(value => this.someNumber = value);

    }

}
```

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

```typescript
import { Component }        from '@angular/core';
import { MyServiceService } from '../my-service.service';

@Component({
    selector: 'app-page1',
    templateUrl: './page1.component.html',
    styleUrls: [ './page1.component.scss' ]
})
export class Page1Component {


    /**
     * By passing the service class to the constructor dependency injection
     * will automatically instantiate it for us.
     *
     * @param {MyServiceService} myServiceService class instance reference.
     */
    public constructor(public myServiceService: MyServiceService) {
        
    }

}
```

## The Component Template

```typescript
<h1>Page 1 Component</h1>

<p>Enter a value below and you will see it update in the footer!</p>

<label>Enter some Number:</label>
<input [formControl]="myServiceService.formControl">
```

As you can see by using `[formControl]="myServiceService.formControl"` we are binding the `<input>` attribute `[formControl]` to the `FormControl` instance from our service class.

# See also

This tutorial is accompanied by a [live demo](https://ng-byexamples-component-comms.herokuapp.com/) and [github repository](https://github.com/mateothegreat/ng-byexamples-component-communication).

Questions or Concerns?Contact me at matthew@matthewdavis.io!

{% github https://github.com/mateothegreat/ng-byexamples-component-communication %}