SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariSurekha Gadkari
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - IntroductionWebStackAcademy
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular ComponentsSquash Apps Pvt Ltd
 
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 HandlingWebStackAcademy
 
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 components
Angular componentsAngular components
Angular componentsSultan Ahmed
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency InjectionNir Kaufman
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorialRohit Gupta
 
Building blocks of Angular
Building blocks of AngularBuilding blocks of Angular
Building blocks of AngularKnoldus Inc.
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & AnswersRatnala Charan kumar
 

Mais procurados (20)

Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular Introduction By Surekha Gadkari
Angular Introduction By Surekha GadkariAngular Introduction By Surekha Gadkari
Angular Introduction By Surekha Gadkari
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
Angular
AngularAngular
Angular
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Sharing Data Between Angular Components
Sharing Data Between Angular ComponentsSharing Data Between Angular Components
Sharing Data Between Angular Components
 
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
 
Angular 9
Angular 9 Angular 9
Angular 9
 
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 components
Angular componentsAngular components
Angular components
 
Angular overview
Angular overviewAngular overview
Angular overview
 
Angular 14.pptx
Angular 14.pptxAngular 14.pptx
Angular 14.pptx
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
 
Angular Dependency Injection
Angular Dependency InjectionAngular Dependency Injection
Angular Dependency Injection
 
Angular tutorial
Angular tutorialAngular tutorial
Angular tutorial
 
Building blocks of Angular
Building blocks of AngularBuilding blocks of Angular
Building blocks of Angular
 
jQuery
jQueryjQuery
jQuery
 
Advanced angular
Advanced angularAdvanced angular
Advanced angular
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & Answers
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

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
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
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
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
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
 
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
 
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.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
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
 
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
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
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
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 

Último (20)

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
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
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 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
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
 
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
 
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 ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
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
 
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
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 

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