What are the main types of data binding in Angular?
Short answer
Angular supports interpolation for displaying a value in text, property binding for setting an element's property, event binding for responding to user actions, and two way binding for keeping a value and an input field in sync in both directions at once.
Advertisement
AModel answer
Angular supports interpolation for displaying a value in text, property binding for setting an element's property, event binding for responding to user actions, and two way binding for keeping a value and an input field in sync in both directions at once.
@Component({
selector: 'app-example',
template: `
<p>{{ message }}</p> <!-- interpolation -->
<img [src]="imageUrl" /> <!-- property binding -->
<button (click)="onClick()">Click</button> <!-- event binding -->
<input [(ngModel)]="name" /> <!-- two way binding -->
`
})
export class ExampleComponent {
message = 'Hello';
imageUrl = 'logo.png';
name = '';
onClick() { console.log('Clicked'); }
}Real-world example
A profile page combines all four binding types in one form, displaying the current name with interpolation, showing a profile picture with property binding, responding to a save button with event binding, and syncing a name input field with two way binding.
Likely follow-ups
- What is the difference between property binding using square brackets and simply setting an HTML attribute?
- What module needs to be imported to use two way binding with ngModel?
Related topics