Angular Data Binding Demo

Here I am demonstrating data binding in a small application. Data binding automatically keeps your page up-to-date based on your application’s state (direct quote from the Angular docs). Essentially, data binding makes our applications more dynamic a…


This content originally appeared on DEV Community and was authored by Chukwuma Anyadike

Here I am demonstrating data binding in a small application. Data binding automatically keeps your page up-to-date based on your application's state (direct quote from the Angular docs). Essentially, data binding makes our applications more dynamic and interesting. There are two broad categories of data binding in Angular. There is one-way data binding and two-way data binding. One way data binding can be categorized on whether it is data source to view target (source to view) or view target to data source (view to source).

One-way data binding

Interpolation: source to view. Double curly brace syntax is used like this {{valueToBeInterpolated}}.

//one-way data binding, source-to-view: interpolation

//app.component.ts
title = 'Data Binding Demo';

//app.component.html
<h1>{{title}}</h1>

Property binding: source to view. Square bracket syntax used like this [dataToBind].

//one-way data binding, source-to-view: property binding

//app.component.ts
currentImage = 'https://someImageURL';
isDangerous = false;

//app.component.html
<img [src]="currentImage">
<h2 [style.color]="isDangerous ? 'red' : 'blue' ">{{isDangerous ? 'Warning Danger!' : 'It\'s All Good!'}}</h2>

Event binding: view to source. The event binding syntax uses the paretheses like this (event)='someFunction()'.

//one-way data binding, view-to-source: event binding

//app.component.ts
toggleMessage() {
   this.isDangerous = !this.isDangerous;
};
toggleImage() {
   this.currentImage = this.currentImage===this.dataBindingImage1 ? this.dataBindingImage2 : this.dataBindingImage1
};

//app.component.html
<button mat-raised-button (click)="toggleMessage()">Toggle Alert</button>
<button mat-raised-button (click)="toggleImage()">Toggle Image</button>

Two-way data binding: source to view and view to source.

The syntax is [(ngModel)]="inputText". This is also known as 'banana in a box' syntax.

//two-way data binding, view-to-source and source-to-view

//app.component.ts
inputText = '';

//app.component.html
<input [(ngModel)]="inputText">

Here is a nice demo which demonstrates these principles. Just press play. For more information about data binding: Data Binding In Angular. The code for this demo is available here: Data Binding Demo. Happy Coding.


This content originally appeared on DEV Community and was authored by Chukwuma Anyadike


Print Share Comment Cite Upload Translate Updates
APA

Chukwuma Anyadike | Sciencx (2024-08-06T01:31:12+00:00) Angular Data Binding Demo. Retrieved from https://www.scien.cx/2024/08/06/angular-data-binding-demo-2/

MLA
" » Angular Data Binding Demo." Chukwuma Anyadike | Sciencx - Tuesday August 6, 2024, https://www.scien.cx/2024/08/06/angular-data-binding-demo-2/
HARVARD
Chukwuma Anyadike | Sciencx Tuesday August 6, 2024 » Angular Data Binding Demo., viewed ,<https://www.scien.cx/2024/08/06/angular-data-binding-demo-2/>
VANCOUVER
Chukwuma Anyadike | Sciencx - » Angular Data Binding Demo. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/06/angular-data-binding-demo-2/
CHICAGO
" » Angular Data Binding Demo." Chukwuma Anyadike | Sciencx - Accessed . https://www.scien.cx/2024/08/06/angular-data-binding-demo-2/
IEEE
" » Angular Data Binding Demo." Chukwuma Anyadike | Sciencx [Online]. Available: https://www.scien.cx/2024/08/06/angular-data-binding-demo-2/. [Accessed: ]
rf:citation
» Angular Data Binding Demo | Chukwuma Anyadike | Sciencx | https://www.scien.cx/2024/08/06/angular-data-binding-demo-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.