You can setup two-way data binding between parent and child component in the following ways:
<app-child [(counter)]="counter"></app-child>
<app-child [counter]="counter" (counterChange)="counter=$event"></app-child>
<app-child [counter]="counter" (counterChange)="onCounterChange($event)"></app-child>
According to Angular - Template Syntax - Two-way binding:
Angular offers a special two-way data binding syntax for this purpose,
[(x)]. The [(x)] syntax combines the brackets of property binding,
[x], with the parentheses of event binding, (x).
<app-child [(counter)]="counter"></app-child>
The [(x)] syntax is easy to demonstrate when the element has a
settable property called x and a corresponding event named xChange.
@Input() counter: number;
@Output() counterChange = new EventEmitter<number>();
The two-way binding syntax is really just syntactic sugar for a
property binding and an event binding. Angular desugars the
ChildComponent binding into this:
<app-child [counter]="counter" (counterChange)="counter=$event"></app-child>
Example: https://stackblitz.com/edit/angular-two-way-data-binding-between-parent-and-child-component?file=src%2Fapp%2Fapp.component.ts
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<div style="background-color: red; padding: 10px;">
<div>{{counter}}</div>
<button (click)="increment()">increment from parent</button>
<app-child [(counter)]="counter"></app-child>
<app-child [counter]="counter" (counterChange)="counter=$event"></app-child>
<app-child [counter]="counter" (counterChange)="onCounterChange($event)"></app-child>
</div>
`
})
export class AppComponent {
counter = 0;
increment() {
this.counter++;
}
onCounterChange(counter: number) {
this.counter = counter;
}
}
@Component({
selector: 'app-child',
template: `
<div style="background-color: green; padding: 10px; margin: 10px;">
<div>{{counter}}</div>
<button (click)="increment()">increment from child</button>
</div>
`,
})
export class ChildComponent {
@Input() counter: number;
@Output() counterChange = new EventEmitter<number>();
constructor() { }
increment() {
this.counterChange.emit(++this.counter);
}
}