Introduction to Data binding
DATA BINDING IN ANGULAR

Introduction to Data binding

Data-binding is the mechanism for coordinating the parts of a template with the parts of the component. You can achieve data binding by adding the binding markup to the template HTML. This is how Angular gets to know how a template is connected to the component.

There are four types of data binding in Angular:

  • Interpolation
  • Property Binding
  • Event Binding
  • Two-Way Data Binding

Interpolation

Interpolation is used to directly access the properties present in a component into the HTML template.It is used to resolve an expression.

  • It can resolve concatenation or numerical expressions.
  • It can resolve the properties defined in the class having scope as public or default.
  • Interpolation don't work for properties marked as private in component class.

The following example demonstrates interpolation.

The output can be seen on the browser as:

As can be seen from the output plus operator with a numeric value performs addition while with string value performs concatenation.

Property Binding

Property Binding is a single way data binding in which you can bind your class level properties to the components view.

The syntax for property binding is:

[attribute-name] = 'property-name'  OR???  [ngModel] = 'property-name'

For Example:

Any change in title property will change the input value, however reverse is not possible since it is single way databinding.

Please note, to use ngModel you need to import FormsModule in your root Module.

Event Binding

Event Binding provides you with a way to capture various types of events and execute some functionality if such events occur.

The syntax for event binding is:

(eventNameToBeCaptured) = "someFunction()" or (ngModelChange) = "someFunction()"

Please note, to use ngModelChange you need to import FormsModule in your root Module.

For Example:

Two-Way Data Binding

Two way data binding can bind class level property with component view and any change from either end will be reflected on both the sides instantly.

The syntax for two-way data binding is:

[(attribute-name)] ='properties-name'

For Example:

Any change in the input value of two-way data binding will also change the value displayed using interpolation and vice versa.

<input type="text" [(ngModel)] ="title1"

The above code is equivalent to:

<input [ngModel]="title1" (ngModelChange)="title1=$event">

You can find the reference of above example here: https://github.com/nagmabano/AngularApplication/tree/master/my-app/src/app/header

This was all about the types of data binding in Angular. Feel free to share your experiences in the comments section while working with Angular's data binding.

要查看或添加评论,请登录

Nagma Bano的更多文章

  • Angular Lifecycle Hooks

    Angular Lifecycle Hooks

    Angular Lifecycle Hooks are the different phases of the component from its creation to its destruction where we can…

  • Data Communication In Angular

    Data Communication In Angular

    Data sharing is one of the important concept to understand in Angular. How Angular communicates data from and to…

    4 条评论
  • Modules in Angular

    Modules in Angular

    Modules are a great way to organize an application and extend it with capabilities from external libraries. NgModules…

  • Templating, Styling and Component Hierarchy in Angular

    Templating, Styling and Component Hierarchy in Angular

    In Angular, your views are defined within HTML templates. A template is a form of HTML that tells Angular how to render…

    1 条评论
  • Introduction to Components

    Introduction to Components

    The core concept of any Angular application is the component. In effect, the whole application can be modeled as a tree…

    1 条评论
  • Bootstrapping of the Angular Application

    Bootstrapping of the Angular Application

    Seeing the complexity of the Angular file structure, the first question that comes to the mind is how does it actually…

    1 条评论
  • Angular Architecture

    Angular Architecture

    The main building blocks of Angular are: Modules Components Templates, Directives, and Data Binding Services and…

    1 条评论
  • Getting Started With Angular

    Getting Started With Angular

    To start working with Angular we need to first set up the development environment. A development environment is one…

    1 条评论
  • Angular Features and Benefits

    Angular Features and Benefits

    Angular is one of the most popular modern day web frameworks available today. Because of the sheer support of Google…

  • Angular6 Vs Angular5

    Angular6 Vs Angular5

    Angular 5 Angular 5 showed up on November 1, 2017 with the promise of speed and a smaller size. The key features of…

社区洞察

其他会员也浏览了