DEV Community

Cover image for Angular Data Binding , one Way - two Way - event Binding
nour
nour

Posted on

Angular Data Binding , one Way - two Way - event Binding

are u diving into Angular and feeling overwhelmed by the concept of data binding ? Data binding lies at the heart of Angular and mastering it can unlock powerful capabilities for ur website let's delve into the fundamentals and advanced techniques of Angular data binding together

Understanding Data Binding in Angular:

data binding establishes a connection between the application UI and its underlying data.. it ensures that any changes in the model state are immediately reflected in the view .. and vice versa, providing a seamless user experience..

Types of Data Binding:

  1. One-Way Data Binding: this involves binding data from the component class to the template view.. changes in the component class reflect in the template but not the other way around
**TypeScript**
export class AppComponent {
    title = 'Welcome';
}

**HTML**
<h1>{{ title }}</h1>
Enter fullscreen mode Exit fullscreen mode
  1. Two-Way Data Binding: this allows data synchronization between the component class and the template .. changes in the template reflect in the component class and vice versa
**TypeScript**
export class AppComponent {
    name = 'Nour Bouch';
}

**HTML**
<input type="text" [(ngModel)]="name">
<p>Hello, {{ name }}</p>

Enter fullscreen mode Exit fullscreen mode

Advanced Data Binding Techniques:

1.** Event Binding:** In Angular u can bind methods to events triggered by user interactions such as clicks keypresses and mouse movements.

**HTML**
<button (click)="onButtonClick()">Click me!</button>

**TypeScript**
export class AppComponent {
    onButtonClick(): void {
        console.log('Button clicked!');
    }
}

Enter fullscreen mode Exit fullscreen mode
  1. Property Binding: This allows u to set an element's property to the value of a template expression.
**HTML**
<img [src]="imageUrl">

**TypeScript**
export class AppComponent {
    imageUrl = 'path/to/image.jpg';
}

Enter fullscreen mode Exit fullscreen mode

Conclusion

learning data binding in angular is essential for building dynamic and responsive web applications. by understanding the different types of data binding and employing advanced techniques, you can create seamless user experiences and unlock the full potential of Angular development.

Top comments (0)