SlideShare a Scribd company logo
1 of 19
1
HTML Attributes, DOM
Properties and Angular
Data Binding
Discussion Agenda
• HTML Attributes and DOM Properties
• 3 Types of Data Binding
• Data Binding Targets
• One Way Binding
• Two Way Binding
• NgModel
2
Background – Data Binding
Data binding is a mechanism for coordinating what users see, with application
data values.
In the 90s, before data binding, we had to manually get and set values on our HTML
DOM in order to do this:
var getIt = document.getElementById(‘#elementToUpdate’);
getIt.innerHTML = ‘<p> No matter what, Douglas Crockford is cool </p>’
In the naughts, jQuery kindof helped us. It accomplished the above in a shorthand
format:
var $getIt = $(‘#elementToUpdate’).val();
$(‘#elementToUpdate’).html( ‘<em> jQuery was big in 2009! </em>’);
We can accomplish the above with Angular syntax in our templates, stay tuned!
3
Background: HTML Attributes and DOM Properties
• Attributes are defined by HTML. Properties are defined by the
DOM.
• Attributes initialize DOM properties and then they are done.
Property values can change; attribute values are static.
• Remember, the browser creates the DOM in memory by parsing
your HTML file on page load!
4
Background: HTML Attributes and DOM Properties
Example: Value attribute
1. HTML file contains tag <input type=‘text’ value=“Kirby /’>
2. User types “Meta Knight” in the input box
3. input.getAttribute( ‘value’ ) returns “Kirby”
The HTML attribute value sets the initial value. The DOM value property (what is
rendered) contains the current value
Example: Disabled attribute
<button disabled=‘false’> This button is always disabled </button>
The disabled attribute always initializes the DOM disabled property to true, no matter
what!
5
Background: HTML Attributes and DOM Properties
Angular template data binding works with DOM properties and events. Not
HTML attributes.
“ In the world of Angular, the only role of attributes is to initialize element
and directive state. When you write a data binding, you are dealing
exclusively with properties and events of the target object. HTML attributes
effectively disappear.”
DOM Controller
Events Values
Properties Functions
Angular Data Binding Statements bind DOM properties and events to
controller values and functions.
6
3 Types of Data Binding
7
Data Source = Typescript Controller View Target = HTML
Data Binding Targets
8
Category Type Example
Two Way Two Way <input [( ngModel )] = “name” />
One Way
target to source
Event <button ( click ) = “onClick() > Save
</button>
One Way
source to target
Property <img [ src ] = “imageUrlFromController” >
One Way
source to target
Class <div [ class.special ]="isSpecial">Special</div>
One Way
source to target
Style <button [ style.color ]="isSpecial ? 'red' : 'green'">
One Way
source to target
Attribute <button [ attr.aria-label ] ="help">help</button>
One Way Binding – Property Binding
• Property Binding is used to set the value of the target property of an element using
the syntax below.
• Property binding can only set the value of the target property. It cannot read values
or react to events.
<img [ src ]= "heroImageUrl“ >
<img bind-src= "heroImageUrl“ >
9
Target property
Value defined in
controller
One Way Binding – Property Binding and Inputs
• Angular matches the target property name to an input on the target controller.
• Use the inputs array or the @Input decorator to map target property names to controller
properties.
<app-hero-child *ngFor="let hero of heroes“ [hero]="hero“ [master]="master"></app-hero-child>
import { Component, Input } from '@angular/core';
import { Hero } from './hero';
@Component({
selector: 'app-hero-child’,
template: `<h3>{{hero.name}} says:</h3> <p>I, {{hero.name}}, am at your service,
{{masterName}}.</p> `
})
export class HeroChildComponent {
@Input() hero: Hero;
@Input('master') masterName: string;
}
10
One Way Binding – Property Binding with Expressions
<div [ ngClass ]= "classes“ >[ngClass] binding to the classes property</div>
The value string can contain an expression.
• Make sure the expression evaluates to the proper type. (often a
string).
• Don’t forget the proper bracket (or “bind-”) syntax to avoid errors!
<div [ ngClass ]= " 1 > 2 ? class-a : class -b“ >expression example</div>
11
Property Binding or Interpolation?
When working with strings, interpolation and property binding are
identical!
<p><img src="{{heroImageUrl}}"> is the <i>interpolated</i> image.</p>
<p><img [src]="heroImageUrl"> is the <i>property bound</i> image.</p>
<p><span>"{{title}}" is the <i>interpolated</i> title.</span></p>
<p>"<span [innerHTML]="title"></span>" is the <i>property bound</i>
title.</p>
12
One Way Binding – Attribute Binding
• You can use one way binding to set an attribute value if it does not map to a native HTML
element property. In this case, you are creating and setting the attribute.
• Attribute binding is the exception to the rule that a binding can only set a DOM property.
• Attribute binding can only be used with attributes that do not correspond to element
properties: Aria, SVG, table span attributes
<tr><td [ attr.colspan ] ="1 + 1">One-Two</td></tr>
<button [ attr.aria-label ] ="actionName">{{actionName}} with Aria</button>
13
One Way Binding – Class Binding
• Class binding is unique.
• You can replace the class attribute with a string of the class names:
<div class="bad curly special“ [ class ]="badCurly“ >Bad curly</div>
• You can also use the class attribute name to index into the classes array and access unique
values:
<!-- toggle the "special" class on/off with a property -->
<div [ class.special ]= "isSpecial">The class binding is special</div>
<!-- binding to `class.special` trumps the class attribute -->
<div class="special"
[ class.special ]= "!isSpecial">This one is not so special</div>
14
One Way Binding – Style Binding
• Use the prefix ‘style’ and the name of a CSS style property:
[ style.style-property ] = ‘set-to-value’
<button [ style.color ] = "isSpecial ? 'red': 'green'">Red</button>
<button [ style.background-color ] = "canSave ? 'cyan': 'grey'" >Save</button>
15
One Way Binding – Event Binding
• Event binding listens for events on the view target and reacts to them via a template
statement executed by the controller.
• Event binding syntax consists of a target event name within parentheses on the left of an
equal sign, and a quoted template statement on the right
<button ( click ) = "onSave()“ >Save</ button>
An alternate, less-used syntax is the canonical ‘on’ prefix:
<button on-click = "onSave()“ > Save </ button>
Angular matches the target event with an element event or output property of the controller.
16
$Event and Event Handling Statements
• In an event binding, Angular sets up an event handler for the target event.
• When the event is raised, the handler executes the template statement.
• The Angular data binding passes information about the event through the $event object
which is a this pointer to the event’s execution scope.
<input [ value ] = "currentHero.name"
( input ) = "currentHero.name = $event.target.value" >
• To update the name property, the changed text is retrieved by following the path
$event.target.value.
• Components can define custom $event objects using the Angular built-in EventEmitter
class.
17
Two Way Binding [ ( ) ]
• Two way data binding is used to both display a data property and update it
when the data changes.
• Two way binding both sets a specific element property and listens for the
element change event.
[ Property Binding ] + ( Event Binding ) = [ ( Two Way Data Binding ) ]
Think “Banana In A Box “
18
Two Way Binding NgModel
• Angular includes built-in attribute directives which watch and modify the
behavior of other HTML elements, attributes, properties, components.
• NgModel creates a two way data binding to an HTML form element.
• Use NgModel to both display a property and update it when the view changes.
• (Don’t forget to import the FormsModule and add it to the NgModule’s imports
list
NgModel = ngModel data prop + ngModelChange event
<input [( ngModel )] = "currentHero.name“ > =
<input [ ngModel ] = “ currentHero.name ” ( ngModelChange ) =
“currentHero.name=$event “ >
The ngModel data property sets the element's value property and the
ngModelChange event property listens for changes to the element's value!
19

More Related Content

What's hot

Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes WorkshopNir Kaufman
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data BindingDuy Khanh
 
Angular data binding
Angular data binding Angular data binding
Angular data binding Sultan Ahmed
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariSurekha Gadkari
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & AnswersRatnala Charan kumar
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Edureka!
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginnersImran Qasim
 
Modules in AngularJs
Modules in AngularJsModules in AngularJs
Modules in AngularJsK Arunkumar
 
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafSpring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafThymeleaf
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming WebStackAcademy
 
Angular Interview Questions-PDF.pdf
Angular Interview Questions-PDF.pdfAngular Interview Questions-PDF.pdf
Angular Interview Questions-PDF.pdfJohnLeo57
 

What's hot (20)

Angular Pipes Workshop
Angular Pipes WorkshopAngular Pipes Workshop
Angular Pipes Workshop
 
Angular 9
Angular 9 Angular 9
Angular 9
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Angular data binding
Angular data binding Angular data binding
Angular data binding
 
Angular introduction students
Angular introduction studentsAngular introduction students
Angular introduction students
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & Answers
 
Angular overview
Angular overviewAngular overview
Angular overview
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginners
 
Angular js PPT
Angular js PPTAngular js PPT
Angular js PPT
 
Modules in AngularJs
Modules in AngularJsModules in AngularJs
Modules in AngularJs
 
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafSpring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Angular Interview Questions-PDF.pdf
Angular Interview Questions-PDF.pdfAngular Interview Questions-PDF.pdf
Angular Interview Questions-PDF.pdf
 

Similar to Angular Data Binding

Fly High With Angular - How to build an app using Angular
Fly High With Angular - How to build an app using AngularFly High With Angular - How to build an app using Angular
Fly High With Angular - How to build an app using AngularVacation Labs
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVMAbhishek Sur
 
Introduction to XAML and its features
Introduction to XAML and its featuresIntroduction to XAML and its features
Introduction to XAML and its featuresAbhishek Sur
 
Magic of web components
Magic of web componentsMagic of web components
Magic of web componentsHYS Enterprise
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...FalafelSoftware
 
Web Components for Java Developers
Web Components for Java DevelopersWeb Components for Java Developers
Web Components for Java DevelopersJoonas Lehtinen
 
SEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePointSEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePointMarc D Anderson
 
JavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeJavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeLaurence Svekis ✔
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript FrameworkAll Things Open
 
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Iasi code camp 12 october 2013   shadow dom - mihai bîrsanIasi code camp 12 october 2013   shadow dom - mihai bîrsan
Iasi code camp 12 october 2013 shadow dom - mihai bîrsanCodecamp Romania
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsMark Rackley
 

Similar to Angular Data Binding (20)

AngularJS
AngularJSAngularJS
AngularJS
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Fly High With Angular - How to build an app using Angular
Fly High With Angular - How to build an app using AngularFly High With Angular - How to build an app using Angular
Fly High With Angular - How to build an app using Angular
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVM
 
Introduction to XAML and its features
Introduction to XAML and its featuresIntroduction to XAML and its features
Introduction to XAML and its features
 
Magic of web components
Magic of web componentsMagic of web components
Magic of web components
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
 
AngularJS
AngularJSAngularJS
AngularJS
 
J query1
J query1J query1
J query1
 
Web Components for Java Developers
Web Components for Java DevelopersWeb Components for Java Developers
Web Components for Java Developers
 
22 j query1
22 j query122 j query1
22 j query1
 
SEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePointSEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePoint
 
Knockout.js
Knockout.jsKnockout.js
Knockout.js
 
JavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeJavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive Code
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
 
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Iasi code camp 12 october 2013   shadow dom - mihai bîrsanIasi code camp 12 october 2013   shadow dom - mihai bîrsan
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
 
SharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentialsSharePoint Cincy 2012 - jQuery essentials
SharePoint Cincy 2012 - jQuery essentials
 

More from Jennifer Estrada

Angular View Encapsulation
Angular View EncapsulationAngular View Encapsulation
Angular View EncapsulationJennifer Estrada
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsJennifer Estrada
 
Domain Driven Design for Angular
Domain Driven Design for AngularDomain Driven Design for Angular
Domain Driven Design for AngularJennifer Estrada
 
Authenticating Angular Apps with JWT
Authenticating Angular Apps with JWTAuthenticating Angular Apps with JWT
Authenticating Angular Apps with JWTJennifer Estrada
 

More from Jennifer Estrada (6)

React JS .NET
React JS .NETReact JS .NET
React JS .NET
 
Angular View Encapsulation
Angular View EncapsulationAngular View Encapsulation
Angular View Encapsulation
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAs
 
Domain Driven Design for Angular
Domain Driven Design for AngularDomain Driven Design for Angular
Domain Driven Design for Angular
 
Authenticating Angular Apps with JWT
Authenticating Angular Apps with JWTAuthenticating Angular Apps with JWT
Authenticating Angular Apps with JWT
 
Angular IO
Angular IOAngular IO
Angular IO
 

Recently uploaded

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 

Recently uploaded (20)

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Angular Data Binding

  • 1. 1 HTML Attributes, DOM Properties and Angular Data Binding
  • 2. Discussion Agenda • HTML Attributes and DOM Properties • 3 Types of Data Binding • Data Binding Targets • One Way Binding • Two Way Binding • NgModel 2
  • 3. Background – Data Binding Data binding is a mechanism for coordinating what users see, with application data values. In the 90s, before data binding, we had to manually get and set values on our HTML DOM in order to do this: var getIt = document.getElementById(‘#elementToUpdate’); getIt.innerHTML = ‘<p> No matter what, Douglas Crockford is cool </p>’ In the naughts, jQuery kindof helped us. It accomplished the above in a shorthand format: var $getIt = $(‘#elementToUpdate’).val(); $(‘#elementToUpdate’).html( ‘<em> jQuery was big in 2009! </em>’); We can accomplish the above with Angular syntax in our templates, stay tuned! 3
  • 4. Background: HTML Attributes and DOM Properties • Attributes are defined by HTML. Properties are defined by the DOM. • Attributes initialize DOM properties and then they are done. Property values can change; attribute values are static. • Remember, the browser creates the DOM in memory by parsing your HTML file on page load! 4
  • 5. Background: HTML Attributes and DOM Properties Example: Value attribute 1. HTML file contains tag <input type=‘text’ value=“Kirby /’> 2. User types “Meta Knight” in the input box 3. input.getAttribute( ‘value’ ) returns “Kirby” The HTML attribute value sets the initial value. The DOM value property (what is rendered) contains the current value Example: Disabled attribute <button disabled=‘false’> This button is always disabled </button> The disabled attribute always initializes the DOM disabled property to true, no matter what! 5
  • 6. Background: HTML Attributes and DOM Properties Angular template data binding works with DOM properties and events. Not HTML attributes. “ In the world of Angular, the only role of attributes is to initialize element and directive state. When you write a data binding, you are dealing exclusively with properties and events of the target object. HTML attributes effectively disappear.” DOM Controller Events Values Properties Functions Angular Data Binding Statements bind DOM properties and events to controller values and functions. 6
  • 7. 3 Types of Data Binding 7 Data Source = Typescript Controller View Target = HTML
  • 8. Data Binding Targets 8 Category Type Example Two Way Two Way <input [( ngModel )] = “name” /> One Way target to source Event <button ( click ) = “onClick() > Save </button> One Way source to target Property <img [ src ] = “imageUrlFromController” > One Way source to target Class <div [ class.special ]="isSpecial">Special</div> One Way source to target Style <button [ style.color ]="isSpecial ? 'red' : 'green'"> One Way source to target Attribute <button [ attr.aria-label ] ="help">help</button>
  • 9. One Way Binding – Property Binding • Property Binding is used to set the value of the target property of an element using the syntax below. • Property binding can only set the value of the target property. It cannot read values or react to events. <img [ src ]= "heroImageUrl“ > <img bind-src= "heroImageUrl“ > 9 Target property Value defined in controller
  • 10. One Way Binding – Property Binding and Inputs • Angular matches the target property name to an input on the target controller. • Use the inputs array or the @Input decorator to map target property names to controller properties. <app-hero-child *ngFor="let hero of heroes“ [hero]="hero“ [master]="master"></app-hero-child> import { Component, Input } from '@angular/core'; import { Hero } from './hero'; @Component({ selector: 'app-hero-child’, template: `<h3>{{hero.name}} says:</h3> <p>I, {{hero.name}}, am at your service, {{masterName}}.</p> ` }) export class HeroChildComponent { @Input() hero: Hero; @Input('master') masterName: string; } 10
  • 11. One Way Binding – Property Binding with Expressions <div [ ngClass ]= "classes“ >[ngClass] binding to the classes property</div> The value string can contain an expression. • Make sure the expression evaluates to the proper type. (often a string). • Don’t forget the proper bracket (or “bind-”) syntax to avoid errors! <div [ ngClass ]= " 1 > 2 ? class-a : class -b“ >expression example</div> 11
  • 12. Property Binding or Interpolation? When working with strings, interpolation and property binding are identical! <p><img src="{{heroImageUrl}}"> is the <i>interpolated</i> image.</p> <p><img [src]="heroImageUrl"> is the <i>property bound</i> image.</p> <p><span>"{{title}}" is the <i>interpolated</i> title.</span></p> <p>"<span [innerHTML]="title"></span>" is the <i>property bound</i> title.</p> 12
  • 13. One Way Binding – Attribute Binding • You can use one way binding to set an attribute value if it does not map to a native HTML element property. In this case, you are creating and setting the attribute. • Attribute binding is the exception to the rule that a binding can only set a DOM property. • Attribute binding can only be used with attributes that do not correspond to element properties: Aria, SVG, table span attributes <tr><td [ attr.colspan ] ="1 + 1">One-Two</td></tr> <button [ attr.aria-label ] ="actionName">{{actionName}} with Aria</button> 13
  • 14. One Way Binding – Class Binding • Class binding is unique. • You can replace the class attribute with a string of the class names: <div class="bad curly special“ [ class ]="badCurly“ >Bad curly</div> • You can also use the class attribute name to index into the classes array and access unique values: <!-- toggle the "special" class on/off with a property --> <div [ class.special ]= "isSpecial">The class binding is special</div> <!-- binding to `class.special` trumps the class attribute --> <div class="special" [ class.special ]= "!isSpecial">This one is not so special</div> 14
  • 15. One Way Binding – Style Binding • Use the prefix ‘style’ and the name of a CSS style property: [ style.style-property ] = ‘set-to-value’ <button [ style.color ] = "isSpecial ? 'red': 'green'">Red</button> <button [ style.background-color ] = "canSave ? 'cyan': 'grey'" >Save</button> 15
  • 16. One Way Binding – Event Binding • Event binding listens for events on the view target and reacts to them via a template statement executed by the controller. • Event binding syntax consists of a target event name within parentheses on the left of an equal sign, and a quoted template statement on the right <button ( click ) = "onSave()“ >Save</ button> An alternate, less-used syntax is the canonical ‘on’ prefix: <button on-click = "onSave()“ > Save </ button> Angular matches the target event with an element event or output property of the controller. 16
  • 17. $Event and Event Handling Statements • In an event binding, Angular sets up an event handler for the target event. • When the event is raised, the handler executes the template statement. • The Angular data binding passes information about the event through the $event object which is a this pointer to the event’s execution scope. <input [ value ] = "currentHero.name" ( input ) = "currentHero.name = $event.target.value" > • To update the name property, the changed text is retrieved by following the path $event.target.value. • Components can define custom $event objects using the Angular built-in EventEmitter class. 17
  • 18. Two Way Binding [ ( ) ] • Two way data binding is used to both display a data property and update it when the data changes. • Two way binding both sets a specific element property and listens for the element change event. [ Property Binding ] + ( Event Binding ) = [ ( Two Way Data Binding ) ] Think “Banana In A Box “ 18
  • 19. Two Way Binding NgModel • Angular includes built-in attribute directives which watch and modify the behavior of other HTML elements, attributes, properties, components. • NgModel creates a two way data binding to an HTML form element. • Use NgModel to both display a property and update it when the view changes. • (Don’t forget to import the FormsModule and add it to the NgModule’s imports list NgModel = ngModel data prop + ngModelChange event <input [( ngModel )] = "currentHero.name“ > = <input [ ngModel ] = “ currentHero.name ” ( ngModelChange ) = “currentHero.name=$event “ > The ngModel data property sets the element's value property and the ngModelChange event property listens for changes to the element's value! 19