How to Data Bind in Angular 17 - Frontend - A Expert Frontend Developer Must Knew it
Data Binding :
Data binding in Angular refers to the automatic synchronization of data between the model (component) and the view (template). It allows you to establish a connection between the DOM and the component's properties or methods. There are four types of data binding in Angular:
Types of Data Binding :
1. Interpolation ({{ }}): It binds data from the component to the view, displaying component properties in the HTML template.
2. Property Binding ([ ]): It binds data from the component to the DOM properties or attributes, allowing you to set properties dynamically.
3. Event Binding (( )): It binds events from the DOM to the component, allowing you to handle user events like click, input, etc., in the component.
4. Two-Way Data Binding ([( )]): It combines property binding and event binding, allowing data to flow both from the component to the view and from the view to the component, providing a seamless synchronization between the two.
Example:
import { Component } from '@angular/core';
@Component({
selector: 'app-user'
})
export class UserComponent {
title = 'Angular Data Binding';
name = 'John Doe';
message = '';
onInputChange(value: string) {
this.name = value;
}
onClick() {
this.message = 'Button clicked!';
}
}
<h1>{{ title }}</h1>
<input type="text" [value]="name" (input)="onInputChange($event.target.value)">
<button (click)="onClick()">Click Me</button>
<p>{{ message }}</p>
Comments
Post a Comment