SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
Duy Khanh
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
iFour Technolab Pvt. Ltd.
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
Malla Reddy University
 
jQuery
jQueryjQuery
jQuery
Dileep Mishra
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Amit Tyagi
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
Mohammed Arif
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
Rohit Gupta
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
Sunny Sharma
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
Tariqul islam
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
WebStackAcademy
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
Angular 8
Angular 8 Angular 8
Angular 8
Sunil OS
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
WebStackAcademy
 
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!
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
Duy Khanh
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
Eyal Vardi
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
Squash Apps Pvt Ltd
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Amit Tyagi
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
Mohammed Arif
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
Rohit Gupta
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
Sunny Sharma
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
Tariqul islam
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
WebStackAcademy
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
Angular 8
Angular 8 Angular 8
Angular 8
Sunil OS
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
WebStackAcademy
 
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!
 

Similar to Angular Data Binding (20)

AngularJS
AngularJSAngularJS
AngularJS
LearningTech
 
Angular
AngularAngular
Angular
LearningTech
 
Angular
AngularAngular
Angular
LearningTech
 
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
Vacation Labs
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
Salvatore Fazio
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVM
Abhishek Sur
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Introduction to XAML and its features
Introduction to XAML and its featuresIntroduction to XAML and its features
Introduction to XAML and its features
Abhishek Sur
 
Magic of web components
Magic of web componentsMagic of web components
Magic of web components
HYS 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
 
AngularJS
AngularJSAngularJS
AngularJS
Srivatsan Krishnamachari
 
J query1
J query1J query1
J query1
Manav Prasad
 
Web Components for Java Developers
Web Components for Java DevelopersWeb Components for Java Developers
Web Components for Java Developers
Joonas Lehtinen
 
22 j query1
22 j query122 j query1
22 j query1
Fajar Baskoro
 
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-118CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
Sivakumar M
 
SEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePointSEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePoint
Marc D Anderson
 
Knockout.js
Knockout.jsKnockout.js
Knockout.js
Vivek Rajan
 
JavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeJavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive Code
Laurence Svekis ✔
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
All Things Open
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
vrluckyin
 
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
Vacation Labs
 
The Magic of WPF & MVVM
The Magic of WPF & MVVMThe Magic of WPF & MVVM
The Magic of WPF & MVVM
Abhishek Sur
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Introduction to XAML and its features
Introduction to XAML and its featuresIntroduction to XAML and its features
Introduction to XAML and its features
Abhishek Sur
 
Magic of web components
Magic of web componentsMagic of web components
Magic of web components
HYS 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 Developers
Joonas Lehtinen
 
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-118CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
18CSC311J WEB DESIGN AND DEVELPMENT UNIT-1
Sivakumar M
 
SEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePointSEF2013 - A jQuery Primer for SharePoint
SEF2013 - A jQuery Primer for SharePoint
Marc D Anderson
 
JavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeJavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive Code
Laurence Svekis ✔
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
All Things Open
 
Ad

More from Jennifer Estrada (6)

React JS .NET
React JS .NETReact JS .NET
React JS .NET
Jennifer Estrada
 
Angular View Encapsulation
Angular View EncapsulationAngular View Encapsulation
Angular View Encapsulation
Jennifer 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 SPAs
Jennifer Estrada
 
Domain Driven Design for Angular
Domain Driven Design for AngularDomain Driven Design for Angular
Domain Driven Design for Angular
Jennifer Estrada
 
Authenticating Angular Apps with JWT
Authenticating Angular Apps with JWTAuthenticating Angular Apps with JWT
Authenticating Angular Apps with JWT
Jennifer Estrada
 
Angular IO
Angular IOAngular IO
Angular IO
Jennifer Estrada
 
Angular View Encapsulation
Angular View EncapsulationAngular View Encapsulation
Angular View Encapsulation
Jennifer 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 SPAs
Jennifer Estrada
 
Domain Driven Design for Angular
Domain Driven Design for AngularDomain Driven Design for Angular
Domain Driven Design for Angular
Jennifer Estrada
 
Authenticating Angular Apps with JWT
Authenticating Angular Apps with JWTAuthenticating Angular Apps with JWT
Authenticating Angular Apps with JWT
Jennifer Estrada
 
Ad

Recently uploaded (20)

Gojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service BusinessGojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service Business
XongoLab Technologies LLP
 
How to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdfHow to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdf
victordsane
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with PrometheusMeet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Eric D. Schabell
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
How to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdfHow to avoid IT Asset Management mistakes during implementation_PDF.pdf
How to avoid IT Asset Management mistakes during implementation_PDF.pdf
victordsane
 
Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509Orion Context Broker introduction 20250509
Orion Context Broker introduction 20250509
Fermin Galan
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with PrometheusMeet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Meet the New Kid in the Sandbox - Integrating Visualization with Prometheus
Eric D. Schabell
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 

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
  翻译: