04 Data Binding
04 Data Binding
Data Binding
Agenda
The Importance of Binding
Templates
Component to View
– Types of Bindings
– Using Structural Directives
– Template Reference Variables
– Value Conversion
View To Component
– Handling Events
– Two-Way Data Binding
The need for Data Binding
A world without data binding
– Load data properties into the DOM
– Allow the user changes
– Synchronize your data objects with the DOM
– Casting from text to other types
DataBinding allows
– Using MV* pattern to separate View from data and logic
– Declarative synchronization between data and view
Model-View-ViewModel
Page
Presentation
+ Logic
+ Events
Page
Pure HTML
Relies heavily on data binding
– To data
– To functions
ViewModel
A ViewModel is an object that contains all of the data(models) and
functionality that a view binds to
In Angular, the component serves as a ViewModel for its Template
element
Command
Function
DOMData
Data
Data
Data
Data
Data
Data
Data
Data
View ViewModel Model
Templates
A template is a piece of HTML with 'markers'
These 'markers' are compiled by Angular
– We call them Directives
E.g. they can be used for Data Binding
One-way Binding
<div>
<p>Welcome {{user.username}}</p> Two-way Binding
<input [(ngModel)]="user.username" />
<input type="password" [(ngModel)]="user.password" />
<input type="button" (click)="login()" value="login" />
</div>
Bind to a function
Components can contain other components to create complex hierarchies
Templates
Templates can be specified in multiple ways:
– Using a URL to an HTML file
– Inline
To facilitate inline template you can use back ticks to write templates on
multiple lines
@Component({
template: `
<div>
<p>Welcome {{user.username}}</p>
<input [(ngModel)]="user.username" />
<input type="password" [(ngModel)]="user.password" />
<input type="button" (click)="login()" value="login"/>
</div>
`
})
Once Again
Back Ticks, NOT single quotes
– With quotes you have to write everything on one line
` ≠ '
Component View
One-way bindings update the View from the Component
– No side effects are allowed
– In other words View cannot update Component using this mechanism
Types
– Interpolation
– Property Binding
– Attribute
Component View
Interpolation
– format:{{template expression}}
– Displays data from the component
<p>5+25={{5+25}}</p>
Template Expressions
JS expressions with side effects are NOT valid template expressions
– assignments (=, +=, -=)
– the 'new' operator
– chaining expressions with ; or ,
– increment and decrement operators (++ and --)
E.g.
– firstName is allowed to be null or undefined blank
– If owner is null error!
This can also be used to pass along complex data to another component
– Only used as input, not as in-out
<pizza-detail [pizza]="current"></pizza-detail>
Property Binding
Template expression may also hold a directive
– Angular will always try to match a directive first, if not known, will assume that it's a
property
attribute
<input type="checkbox" disabled="disabled"/>
boolean!
Property Binding
Property Binding versus Interpolation
Creates local
variable
<tbody>
<tr *ngFor="let pizza of pizzas">
<td>{{pizza.name}}</td>
<td [class.promo]="pizza.isPromo">{{pizza.price}}</td>
<td>{{pizza.spiciness}}</td> Binds to item
</tr>
</tbody>
Template Reference Variables
A Template Reference Variable is a variable created in a template and
references data
– Syntax: #identifier
Example
<input #newPizza>
<button (click)="addPizza(newPizza.value)">Add</button>
Template Reference Variables
Where does the value come from?
– If a directive is present, the directive is responsible for setting the value
<input [(ngModel)]="tshirt.email"
name="email" type="email"
#email="ngModel">
<input #newPizza>
Value Conversion
Binding result depends on a Model’s value, but is not the value itself
– Use a converter function
– function is reevaluated when dependency gets modified
<td>{{pizza.price | currency:'EUR':'symbol':'1.2-
2'}}</td>
usage of
the currency pipe
"10" "€10.00"
View Component
Bindings to call the Component from the View
– Side effects are allowed
– Models might be updated
– Certain views will be re-rendered
Types
– Event handling
– Two-way binding
Event Handling
Respond to events
– (event)="template statement"
<input (click)="add()" type="button" />
An event may be
– Directive event
– DOM event
$event
– Object that contains information about the event
<input (keyup)="onKey($event)">
onKey(event: KeyboardEvent) {
this.values.push((<HTMLInputElement>event.target).value);
}
Template Statements
Are not the same as template expressions!
– Similar, but does allow side effects
– Actually it should have a side effect
– Component instance or template reference variable as context
Allowed
– Chaining expressions with ; or ,
– Pure assignment (=)
Not allowed
– Operator assignments (+=, -=)
– The 'new' operator
– Increment and decrement operators (++ and --)
– No support for bitwise operators | and &
– No access to global variables (document)
Best practice: keep it simple!
Event Handling
$event
– Object that contains information about the event
– Exact type is determined by the raised event
<input (keyup)="onKey($event)">
onKey(event: KeyboardEvent) {
this.values.push((<HTMLInputElement>event.target).value);
}
onKey(value: string) {
this.values.push(value);
}
Event Handling
Key event filtering
– Angular allows us to listen to specific keys instead of all
Multiple bindings
<input #newPizza
(keyup.enter)="addPizza(newPizza.value)"
(blur)="addPizza(newPizza.value)">
<button (click)=addPizza(newPizza.value)>Add</button>
Two-way Binding
Two-way binding is just the combination of one-way binding with event
handling
simplified
<input [(ngModel)]="current.name">
Two-way Binding
First syntax only useful when doing something else than just setting the value
<input [ngModel]="current.name"
(ngModelChange)="validateAndSet($event)">
Remarks
Alternative syntax
– Some people don't like all the [], (), * - stuff
– There is an alternative syntax called canonical syntax
E.g.
– <input #tel> <input ref-tel>
– (click)="add()" on-click="add()"
Summary
The Importance of Binding
– MV* pattern
Component to View
– Types of Bindings
{{}}, [property],[attr.attr-name]
– Using Structural Directives
– Template Reference Variables
– Value Conversion
function, |
View To Component
– Handling Events
(event)
– Two-Way Data Binding
[(banana-in-a-box)]
Summary
{{value}}
[property]="value"
Component
DOM
(event)="handler"
[(ngModel)]="property"