If you’ve ever used [(ngModel)]
with Angular, you know what two-way data binding is. The idea is to have a variable that works simultaneously as an Input and an Output.
In today’s post, we will see how to implement such a two-way data binding on our own so that we can use the [()]
syntax on any of our component properties, such as message
in the following code:
Before we get into this tutorial, I want to tease that signal-based components will have such two-way data bindings available out of the box with the following syntax – using a model()
function instead of the @Input
and @Output
decorators we’re used to:
Note that the above feature will not be a part of the initial developer preview of Angular Signals coming out in less than two weeks with Angular 16. However, it might be available in developer preview in six months with Angular 17.
In any case, here is the link to my custom two-way data binding tutorial and a code example in action on Stackblitz.