You’re probably familiar with [(ngModel)], I’m going to explain you how ngModel actually works, which allow us to replicate its behavior in order to implement our own two-way data binding.
In end of article, we will create a component that takes a message as a parameter and that message will be our component model. We would use that component as follows:
<ex-component [(message)]="title"></ex-component>
Above code is the two-way data binding we’re looking to implement.
<input [(ngModel)]="title" type="text">
Lets's understand syntax [()], which turns out to be syntactic sugar for the following:
<input [ngModel]="title" (ngModelChange)="title= $event" type="text">
Now real secret that the above example unveils about is the use of the Change suffix. So Let’s apply this trick to our example.
First create a simple component with a @Input to have a one-way data binding:
export class TwoWayDataBindingExample {
messageValue : string;
@Input()
get message(){
return this.messageValue;
}
set message(val) {
this.messageValue = val;
}
}
There are two important things that we should care about here. First I uses messageValue to store the actual message. Then I show it as message, so that I can used in TypeScript getters and setters to read and set the value of messageValue.
So now our setter will have to do some more work to actually notify other components of any model update its message.
So second one-way data-binding implementations will be as follows:
export class TwoWayDataBindingExampleComponent {
messageValue : string;
@Output()
messageChange = new EventEmitter<string>();
@Input()
get message(){
return this.messageValue;
}
set message(val) {
this.messageValue = val;
this.messageChange.emit(this.messageValue);
}
}
Now our code become more clear. Both the getter and setter use messageValue to read / update our data model, but the thing is setter has to also notifies to rest of the world by emitting an event.
As a result, we have a two-way data binding applied to component with the following syntax:
<ex-component [(message)]="title"></ex-component>
Now to get notified when message changes:
Wasn’t that easy? Well, it sure is once we know the trick to get there. Now you can implement two-way data-bindings for your own!
In end of article, we will create a component that takes a message as a parameter and that message will be our component model. We would use that component as follows:
<ex-component [(message)]="title"></ex-component>
Above code is the two-way data binding we’re looking to implement.
<input [(ngModel)]="title" type="text">
Lets's understand syntax [()], which turns out to be syntactic sugar for the following:
<input [ngModel]="title" (ngModelChange)="title= $event" type="text">
Now real secret that the above example unveils about is the use of the Change suffix. So Let’s apply this trick to our example.
First create a simple component with a @Input to have a one-way data binding:
export class TwoWayDataBindingExample {
messageValue : string;
@Input()
get message(){
return this.messageValue;
}
set message(val) {
this.messageValue = val;
}
}
There are two important things that we should care about here. First I uses messageValue to store the actual message. Then I show it as message, so that I can used in TypeScript getters and setters to read and set the value of messageValue.
So now our setter will have to do some more work to actually notify other components of any model update its message.
So second one-way data-binding implementations will be as follows:
export class TwoWayDataBindingExampleComponent {
messageValue : string;
@Output()
messageChange = new EventEmitter<string>();
@Input()
get message(){
return this.messageValue;
}
set message(val) {
this.messageValue = val;
this.messageChange.emit(this.messageValue);
}
}
Now our code become more clear. Both the getter and setter use messageValue to read / update our data model, but the thing is setter has to also notifies to rest of the world by emitting an event.
As a result, we have a two-way data binding applied to component with the following syntax:
<ex-component [(message)]="title"></ex-component>
Now to get notified when message changes:
Wasn’t that easy? Well, it sure is once we know the trick to get there. Now you can implement two-way data-bindings for your own!

0 Comments