SlideShare a Scribd company logo
DATA BINDING IN 
ANGULARJS 
FROM MODEL TO VIEW 
Thomas Roch, Formedix Ltd
TYPES OF DATA BINDING 
One way (model to view) 
Two-way data binding
FROM VIEW TO MODEL 
Javascript DOM Event model: Event, KeyboardEvent, MouseEvent... 
Easy to bind model to view in Angular directives 
app.directive('input', function () { 
return { 
restrict: 'E', 
scope: false, 
link: function (scope, element, attrs) { 
element.bind('input', function (event) { 
scope.$apply(function () { 
scope.model = element.val(); 
}); 
}); 
} 
}; 
});
FROM MODEL TO VIEW 
No events or notifications fired when a value changes in EcmaScript 5 
Observers are for the future (EcmaScript 6) 
Object.observe(myObj, function observer(changes) { 
/* Do something */ 
changes.forEach(function (change) { 
console.log(change.name, change.type, change.oldValue); 
}); 
}); 
A WAY TO OVERCOME THIS: DIRTY CHECKING
WATCHERS 
// Object.observe(myObj, function observer(changes) { 
// changes.forEach(function (change) { 
// console.log(change.name, change.type, change.oldValue); 
// }); 
// }); 
$scope.$watch(watchExpression, function listener(newValue, oldValue) { 
// Do something 
console.log(newValue, oldValue); 
}); 
watchExpression can be an expression (string) 
It is evaluated in the context of the $scope using $parse 
Or it can be a function returning a value 
Watched values can be primitives, array or objects 
The listener function is executed if the watchExpression result has 
changed since the last check. 
$scope.$watch() returns a deregistration function
WATCHING DEPTHS 
$scope.$watch(watchExpr, listener) 
for watching primitives or object references 
$scope.$watchCollection(watchExpr, listener) 
for watching additions and deletions in Arrays 
$scope.$watch(watchExpr, listener, true) 
for objects and arrays equality (using angular.equals)
Credits: Tero Parviainen, http://teropa.info/blog/2014/01/26/the-three-watch-depths-of-angularjs.html
WHICH DIRECTIVES ADD WATCHERS? 
$watch: 
{{ }}, ngBind, ngBindTemplate, ngModel 
ngShow, ngHide, ngIf, ngSwitch 
ngSelected, ngChecked, ngDisabled, ngRequired, etc... 
$watchCollection: 
ngRepeat 
$watch with object equality: 
ngClass, ngStyle
THE DIGEST PROCESS 
$scope.$digest() digests a scope and its children 
Cannot run concurently 
Evaluates all watch expressions and functions 
If at least one listener is fired, it will re-evaluate all watch expressions 
The digest process stops when no more listeners have been fired 
Stops at 10 times and throws an infinite digest loop
WHAT TRIGGERS A DIGEST? 
$digest usually not called directly 
// Apply changes: execute function and call $digest 
$scope.$apply(function () { 
$scope.myModel = 'Hello'; 
}); 
Services: $http, $timeout, $interval 
Directives: ngClick, ngChange, ngFocus, ngPaste, etc... 
When inside a watch listener, no need to trigger a digest!
ISSUE #1: PERFORMANCE 
To measure performance improvements we have 
created a crude benchmark of a table of 20 columns (a 
repeater) and 100 rows (another repeater) and placed 
a binding in each cell for a total of 2000 cells. We then 
proceeded to change a small portion of the model to 
see the performance characteristics of the update. The 
result is summarized below. 
Dirty checking: 40ms 
Observers: 1-2ms
ISSUE #2: SUSTAINABILITY 
The more the watchers, the more processing required 
The more processing, the more time it takes
SOLUTION 
Limiting the number of watchers!
SCOPES AND DATA 
A variable can be available when the scope is instanciated: use of 
resolve, or synchronous initialisation 
A variable can be available after the scope is instanciated: fetching data 
asynchronously 
A variable can be changing multiple times during a scope's life
THE THREE CASES OF WATCHING DATA 
A watcher is watching... 
data available at scope creation which will never change 
data available after scope creation which will never change thereafter 
data available immediately or not, which will change once or more
DEMO
QUESTIONS

More Related Content

PPT
Ext J S Observable
jason hu 金良胡
 
PDF
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Nexus FrontierTech
 
PPTX
Knockoutjs Part 2 Beginners
Bhaumik Patel
 
PDF
Reactive computing
Stan Lea
 
PPT
Lect 15-16 Zaheer Abbas
Information Technology Center
 
PDF
redux and angular - up and running
Nir Kaufman
 
PDF
The basics of Ember Objects
Jason Schmidt
 
PPTX
Oracle: Functions
DataminingTools Inc
 
Ext J S Observable
jason hu 金良胡
 
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Nexus FrontierTech
 
Knockoutjs Part 2 Beginners
Bhaumik Patel
 
Reactive computing
Stan Lea
 
Lect 15-16 Zaheer Abbas
Information Technology Center
 
redux and angular - up and running
Nir Kaufman
 
The basics of Ember Objects
Jason Schmidt
 
Oracle: Functions
DataminingTools Inc
 

What's hot (18)

PDF
Testing with Containers
Romain Baugue
 
PDF
Cursors in MySQL
Tharindu Weerasinghe
 
PPTX
Reactive Programming on Android
Guilherme Branco
 
PPTX
Reactive Programming no Android
Guilherme Branco
 
DOCX
Cursor
Jay Patel
 
DOCX
Java Programs
Seetharamaiah Vadde
 
DOCX
How to handle Dynamic Objects with OpKey?
opkey
 
DOC
Modify the bouncing ball example demonstrated/tutorialoutlet
Cleasbyz
 
PDF
Async Testing giving you a sinking feeling
Erin Zimmer
 
PPTX
Redux in Angular2 for jsbe
Brecht Billiet
 
PDF
Testing with Kotlin
Robert Fletcher
 
PDF
The Ring programming language version 1.10 book - Part 96 of 212
Mahmoud Samir Fayed
 
PPTX
MS Sql Server: Doing Calculations With Functions
DataminingTools Inc
 
PPTX
R: Apply Functions
DataminingTools Inc
 
PPT
Evolution of asynchrony in (ASP).NET
Aliaksandr Famin
 
PDF
Beautiful java script
Ürgo Ringo
 
Testing with Containers
Romain Baugue
 
Cursors in MySQL
Tharindu Weerasinghe
 
Reactive Programming on Android
Guilherme Branco
 
Reactive Programming no Android
Guilherme Branco
 
Cursor
Jay Patel
 
Java Programs
Seetharamaiah Vadde
 
How to handle Dynamic Objects with OpKey?
opkey
 
Modify the bouncing ball example demonstrated/tutorialoutlet
Cleasbyz
 
Async Testing giving you a sinking feeling
Erin Zimmer
 
Redux in Angular2 for jsbe
Brecht Billiet
 
Testing with Kotlin
Robert Fletcher
 
The Ring programming language version 1.10 book - Part 96 of 212
Mahmoud Samir Fayed
 
MS Sql Server: Doing Calculations With Functions
DataminingTools Inc
 
R: Apply Functions
DataminingTools Inc
 
Evolution of asynchrony in (ASP).NET
Aliaksandr Famin
 
Beautiful java script
Ürgo Ringo
 
Ad

Similar to Data binding in AngularJS, from model to view (20)

PPTX
Scope demystified - AngularJS
Sumanth krishna
 
PPTX
Angular Data Binding
Duy Khanh
 
PPTX
AngularJs presentation
Phan Tuan
 
PPTX
AngularJS, More Than Directives !
Gaurav Behere
 
PPTX
Angular VS FORWARD
Costas Zarifis
 
PPTX
Angular vs FORWARD
Costas Zarifis
 
PDF
Dragos Rusu - Angular JS - Overcoming Performance Issues - CodeCamp-10-may-2014
Codecamp Romania
 
PDF
AngularJS - Overcoming performance issues. Limits.
Dragos Mihai Rusu
 
PPT
introduction to Angularjs basics
Ravindra K
 
ODP
Appstate
Tomas Kulich
 
PPT
Angular js meetup
Pierre-Yves Gicquel
 
PPT
angularjsmeetup-150303044616-conversion-gate01
Teo E
 
PDF
Javascript Memory leaks and Performance & Angular
Erik Guzman
 
PPTX
Client Best Practices
Yuval Dagai
 
PPTX
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
PPTX
Dive into Angular, part 3: Performance
Oleksii Prohonnyi
 
PPTX
Angularjs Performance
Steven Lambert
 
PDF
AngularJS Workshop
Gianluca Cacace
 
PPTX
Scope.js prsentation
Atishay Baid
 
PPTX
AngularJS: what is underneath the hood
DA-14
 
Scope demystified - AngularJS
Sumanth krishna
 
Angular Data Binding
Duy Khanh
 
AngularJs presentation
Phan Tuan
 
AngularJS, More Than Directives !
Gaurav Behere
 
Angular VS FORWARD
Costas Zarifis
 
Angular vs FORWARD
Costas Zarifis
 
Dragos Rusu - Angular JS - Overcoming Performance Issues - CodeCamp-10-may-2014
Codecamp Romania
 
AngularJS - Overcoming performance issues. Limits.
Dragos Mihai Rusu
 
introduction to Angularjs basics
Ravindra K
 
Appstate
Tomas Kulich
 
Angular js meetup
Pierre-Yves Gicquel
 
angularjsmeetup-150303044616-conversion-gate01
Teo E
 
Javascript Memory leaks and Performance & Angular
Erik Guzman
 
Client Best Practices
Yuval Dagai
 
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
Dive into Angular, part 3: Performance
Oleksii Prohonnyi
 
Angularjs Performance
Steven Lambert
 
AngularJS Workshop
Gianluca Cacace
 
Scope.js prsentation
Atishay Baid
 
AngularJS: what is underneath the hood
DA-14
 
Ad

Recently uploaded (20)

PDF
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
PPTX
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
PPTX
Black Yellow Modern Minimalist Elegant Presentation.pptx
nothisispatrickduhh
 
PPTX
Crypto Recovery California Services.pptx
lionsgate network
 
PPTX
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
PPTX
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
PDF
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
PPTX
The Latest Scam Shocking the USA in 2025.pptx
onlinescamreport4
 
PDF
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
PDF
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
PDF
Slides: PDF Eco Economic Epochs for World Game (s) pdf
Steven McGee
 
PPTX
Slides Powerpoint: Eco Economic Epochs.pptx
Steven McGee
 
PPTX
Parallel & Concurrent ...
yashpavasiya892
 
PPTX
Unlocking Hope : How Crypto Recovery Services Can Reclaim Your Lost Funds
lionsgate network
 
PPTX
SEO Trends in 2025 | B3AITS - Bow & 3 Arrows IT Solutions
B3AITS - Bow & 3 Arrows IT Solutions
 
PDF
5g is Reshaping the Competitive Landscape
Stellarix
 
PPT
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 
PPTX
原版北不列颠哥伦比亚大学毕业证文凭UNBC成绩单2025年新版在线制作学位证书
e7nw4o4
 
PPTX
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
PPTX
办理方法西班牙假毕业证蒙德拉贡大学成绩单MULetter文凭样本
xxxihn4u
 
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
Black Yellow Modern Minimalist Elegant Presentation.pptx
nothisispatrickduhh
 
Crypto Recovery California Services.pptx
lionsgate network
 
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
The Latest Scam Shocking the USA in 2025.pptx
onlinescamreport4
 
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
Slides: PDF Eco Economic Epochs for World Game (s) pdf
Steven McGee
 
Slides Powerpoint: Eco Economic Epochs.pptx
Steven McGee
 
Parallel & Concurrent ...
yashpavasiya892
 
Unlocking Hope : How Crypto Recovery Services Can Reclaim Your Lost Funds
lionsgate network
 
SEO Trends in 2025 | B3AITS - Bow & 3 Arrows IT Solutions
B3AITS - Bow & 3 Arrows IT Solutions
 
5g is Reshaping the Competitive Landscape
Stellarix
 
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 
原版北不列颠哥伦比亚大学毕业证文凭UNBC成绩单2025年新版在线制作学位证书
e7nw4o4
 
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
办理方法西班牙假毕业证蒙德拉贡大学成绩单MULetter文凭样本
xxxihn4u
 

Data binding in AngularJS, from model to view

  • 1. DATA BINDING IN ANGULARJS FROM MODEL TO VIEW Thomas Roch, Formedix Ltd
  • 2. TYPES OF DATA BINDING One way (model to view) Two-way data binding
  • 3. FROM VIEW TO MODEL Javascript DOM Event model: Event, KeyboardEvent, MouseEvent... Easy to bind model to view in Angular directives app.directive('input', function () { return { restrict: 'E', scope: false, link: function (scope, element, attrs) { element.bind('input', function (event) { scope.$apply(function () { scope.model = element.val(); }); }); } }; });
  • 4. FROM MODEL TO VIEW No events or notifications fired when a value changes in EcmaScript 5 Observers are for the future (EcmaScript 6) Object.observe(myObj, function observer(changes) { /* Do something */ changes.forEach(function (change) { console.log(change.name, change.type, change.oldValue); }); }); A WAY TO OVERCOME THIS: DIRTY CHECKING
  • 5. WATCHERS // Object.observe(myObj, function observer(changes) { // changes.forEach(function (change) { // console.log(change.name, change.type, change.oldValue); // }); // }); $scope.$watch(watchExpression, function listener(newValue, oldValue) { // Do something console.log(newValue, oldValue); }); watchExpression can be an expression (string) It is evaluated in the context of the $scope using $parse Or it can be a function returning a value Watched values can be primitives, array or objects The listener function is executed if the watchExpression result has changed since the last check. $scope.$watch() returns a deregistration function
  • 6. WATCHING DEPTHS $scope.$watch(watchExpr, listener) for watching primitives or object references $scope.$watchCollection(watchExpr, listener) for watching additions and deletions in Arrays $scope.$watch(watchExpr, listener, true) for objects and arrays equality (using angular.equals)
  • 7. Credits: Tero Parviainen, http://teropa.info/blog/2014/01/26/the-three-watch-depths-of-angularjs.html
  • 8. WHICH DIRECTIVES ADD WATCHERS? $watch: {{ }}, ngBind, ngBindTemplate, ngModel ngShow, ngHide, ngIf, ngSwitch ngSelected, ngChecked, ngDisabled, ngRequired, etc... $watchCollection: ngRepeat $watch with object equality: ngClass, ngStyle
  • 9. THE DIGEST PROCESS $scope.$digest() digests a scope and its children Cannot run concurently Evaluates all watch expressions and functions If at least one listener is fired, it will re-evaluate all watch expressions The digest process stops when no more listeners have been fired Stops at 10 times and throws an infinite digest loop
  • 10. WHAT TRIGGERS A DIGEST? $digest usually not called directly // Apply changes: execute function and call $digest $scope.$apply(function () { $scope.myModel = 'Hello'; }); Services: $http, $timeout, $interval Directives: ngClick, ngChange, ngFocus, ngPaste, etc... When inside a watch listener, no need to trigger a digest!
  • 11. ISSUE #1: PERFORMANCE To measure performance improvements we have created a crude benchmark of a table of 20 columns (a repeater) and 100 rows (another repeater) and placed a binding in each cell for a total of 2000 cells. We then proceeded to change a small portion of the model to see the performance characteristics of the update. The result is summarized below. Dirty checking: 40ms Observers: 1-2ms
  • 12. ISSUE #2: SUSTAINABILITY The more the watchers, the more processing required The more processing, the more time it takes
  • 13. SOLUTION Limiting the number of watchers!
  • 14. SCOPES AND DATA A variable can be available when the scope is instanciated: use of resolve, or synchronous initialisation A variable can be available after the scope is instanciated: fetching data asynchronously A variable can be changing multiple times during a scope's life
  • 15. THE THREE CASES OF WATCHING DATA A watcher is watching... data available at scope creation which will never change data available after scope creation which will never change thereafter data available immediately or not, which will change once or more
  • 16. DEMO