e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

Angular: One-way and Two-way data binding with example

What is Data binding?

Data binding defines communication between component and view. There are two types of Data binding exist in Angular: One-way and Two-way data binding.

One-way Data Binding

In this binding, data binds from component to view and vice versa. It is unidirectional binding.

From component to View data binding can be achieved by following ways:

  1. Interpolation Binding
  2. Property Binding
  3. Attribute Binding
  4. Class Binding
  5. Style Binding

Interpolation Binding:

In this we are using binding expression {{}} in the project.

Angular: One-way and Two-way data binding with example

Property Binding:

In this we are setting the property of the view element. We can change property value from component.

Angular: One-way and Two-way data binding with example

Attribute Binding:

It is used to set the attribute property of the view element.

Angular: One-way and Two-way data binding with example

Class Binding:

It is used to set the class property of the view element.

Angular: One-way and Two-way data binding with example

Style Binding:

It is used to set the style of the view element.

Angular: One-way and Two-way data binding with example

Form View to Component data binding can be achieved by using event:

Here we can use the click event of the element to make communication from view to component. Example button click execute function to display alert message.

Angular: One-way and Two-way data binding with example

Two-way Data Binding

Two-way data bing helps to exchange data from component to view and view to component. Using (ngMode) we can achieve two-way data binding in Angular. Before using (ngModel) you need to import FormsModule from @angular/forms in app.module.ts file otherwise gives the error “Can’t bind to ‘ngModel’ since it is not a known property of ‘input’”.

Angular: One-way and Two-way data binding with example

Related Posts

Latest Posts

  • All Posts
  • Generative AI
  • manufacturing
  • News
  • Portfolio
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Nigeria

22 Kumasi Crescent, Wuse 2, Abuja, Nigeria.
info@tudip.com