What is data binding in Angular?

Data binding is a core mechanism in Angular that allows the interaction of the application’s user interface (view) with the model. It represents communication between the component and DOM (Document Object Model). Data binding helps the user to manipulate the website elements and see the reflected changes on the DOM.

Key takeaways:

  • Data binding automates the synchronization between UI and data model.

  • One-way data binding is simpler and more predictable.

  • Two-way data binding offers real-time synchronization but can be more complex.

  • Proper use of data binding can significantly reduce boilerplate code.

Introduction to data binding

Data binding is a technique that creates a link between the user interface of an application and its underlying business logic. It automates the process of data synchronization, reducing the amount of boilerplate code developers need to write. By utilizing the data binding, changes in the data model are automatically reflected in the UI, and vice versa.

Forms of data binding in Angular

Data binding exists in two primary forms:

  • One-way data binding: Unidirectional data flow from models to views.

  • Two-way data binding: Bidirectional data flow between models and views.

One-way data binding

There are three types of one-way data binding:

  1. Interpolation: Interpolation binding displays the HTML output based on typescript code. It shows the bound value of an element from the component. In the following typescript code, we have defined three properties.

    export class AppComponent {
      title = 'Databinding';
      course ='Angular';
      image = 'paste the url here'
    }
    

    The HTML file calls the title property using double curly brackets {{}}. The DOM will display the property title value on the website.

    <li>{{title}}</li>
    
  2. Property binding: This binding lets us bind a component’s property to a DOM object. It is used to set the value of properties of HTML elements with the help of square brackets []. For example, look at the declaration of the isDisabled property in the app.component.ts file.

    isDisabled : boolean = true;
    

    Using the disabled property in the respective HTML file:

    <button [disabled]="isDisabled"
    
  3. Event binding: Event binding allows the user to respond to specific actions such as mouse clicks and touches. We can bind events by specifying the target event name and the corresponding method defined in TypeScript. In the below example, click is the target event, and displayList() is the method necessary to respond to the click event.

    <button (click)="displayList()"> List </button>
    

Two-way data binding

Two-way data binding enables the communication between models and views in both directions, i.e., views to models and models to views. Its syntax includes [(ngModel)] where () represents the event binding and [] is used to bind the property. It enables the user on the UI to make changes that will be reflected in the component. user.familyname in the following example is passed as an input value in the app.component.html file. If the user changes its value, it will automatically update the component.

<input [(ngModel)]="familyname" placeholder ="Two-way binding">

Code example

Use the below widget to practice data binding in Angular.

<!-- Interpolation -->
<div>
  <h1>
    {{ title }}!
  </h1>
</div>

<!-- Property Binding -->
<div>
  <button [disabled]="isDiabled">Property Binding</button>
</div>

<br />

<!-- Event Binding:  displayList() method is called in respose to the click event -->
<div>
  <button (click)="displayList()">Event Binding</button>
</div>

<!-- Two-way binding: As the input is changed, the value of the familyname is updated-->
<div>
  <input [(ngModel)]="familyname" placeholder ="Two-way binding"> 
  <br />
  {{familyname}}
</div>

Code explanation

The app.component.html file contains the examples of data binding that have been explained as follows:

  • Line 4: For interpolation, use {{title}}. It renders the value of the title from app.component.ts to UI.

  • Line 10: We are binding the disabled property to the non-string value of isDisabled. When isDisabled = true, the button will be disabled. This is property binding.

  • Line 16: Event binding is explained by this line of code. displayList() method is called in response to the click target event.

  • Line 22: As the user changes to input, the value of the familyname is updated. This is how two-way binding is performed.

Data binding in other frameworks

  • Angular: Angular provides robust support for both one-way and two-way data binding. It uses a template syntax that extends HTML with Angular-specific attributes and elements.

  • React: React primarily uses one-way data binding and promotes single-directional data flow. However, we can achieve two-way binding by combining state and events.

  • Vue.js: Vue.js offers flexible data binding options, including the v-model directive for two-way binding and various directives for one-way binding.

Best practices

Here are some best practices to follow while implementing data binding in Angular:

  • Use one-way data binding for simple, read-only scenarios.

  • Implement two-way data binding judiciously, as it can lead to complex data flows.

  • Keep components small and focused to simplify data binding logic.

  • Utilize computed properties and watchers in frameworks like Vue.js for derived data.

  • Regularly profile the application to ensure data binding isn’t causing performance issues.

Conclusion

Data binding is a powerful feature in modern web development that streamlines the process of connecting UI elements with underlying data models. By understanding the differences between one-way and two-way data binding and their appropriate use cases, developers can create more efficient, maintainable, and responsive applications. As web frameworks continue to evolve, learning data binding techniques will remain an essential skill for front-end developers.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is data binding with an example?

Data binding connects a UI element to a data source. For example, in Angular:

<input [(ngModel)]="username">
<p>Hello, {{ username }}!</p>

Here, the input field is bound to the username variable, and any changes in the input are immediately reflected in the paragraph below.


What is two-way data binding?

Two-way data binding is a technique where data flows in both directions between the component and the view. Changes in the UI are reflected in the component’s data model, and changes in the data model are immediately updated in the UI.


What is the difference between one-way and two-way data binding?

One-way data binding involves data flow in a single direction, typically from the component to the view. Two-way data binding allows data to flow in both directions, synchronizing the component and the view in real-time. One-way binding is simpler and more predictable, while two-way binding offers more dynamic interactions but can be more complex to manage.


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved