SlideShare a Scribd company logo
1 of 17
Download to read offline
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

What's hot

Testing with Containers
Testing with ContainersTesting with Containers
Testing with ContainersRomain Baugue
 
Reactive Programming on Android
Reactive Programming on AndroidReactive Programming on Android
Reactive Programming on AndroidGuilherme Branco
 
Reactive Programming no Android
Reactive Programming no AndroidReactive Programming no Android
Reactive Programming no AndroidGuilherme Branco
 
How to handle Dynamic Objects with OpKey?
How to handle Dynamic Objects with OpKey?How to handle Dynamic Objects with OpKey?
How to handle Dynamic Objects with OpKey?opkey
 
Modify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletModify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletCleasbyz
 
Async Testing giving you a sinking feeling
Async Testing giving you a sinking feelingAsync Testing giving you a sinking feeling
Async Testing giving you a sinking feelingErin Zimmer
 
Redux in Angular2 for jsbe
Redux in Angular2 for jsbeRedux in Angular2 for jsbe
Redux in Angular2 for jsbeBrecht Billiet
 
The Ring programming language version 1.10 book - Part 96 of 212
The Ring programming language version 1.10 book - Part 96 of 212The Ring programming language version 1.10 book - Part 96 of 212
The Ring programming language version 1.10 book - Part 96 of 212Mahmoud Samir Fayed
 
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With FunctionsMS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With FunctionsDataminingTools Inc
 
Evolution of asynchrony in (ASP).NET
Evolution of asynchrony in (ASP).NETEvolution of asynchrony in (ASP).NET
Evolution of asynchrony in (ASP).NETAliaksandr Famin
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java scriptÜrgo Ringo
 

What's hot (18)

Testing with Containers
Testing with ContainersTesting with Containers
Testing with Containers
 
Cursors in MySQL
Cursors in MySQL Cursors in MySQL
Cursors in MySQL
 
Reactive Programming on Android
Reactive Programming on AndroidReactive Programming on Android
Reactive Programming on Android
 
Reactive Programming no Android
Reactive Programming no AndroidReactive Programming no Android
Reactive Programming no Android
 
Cursor
CursorCursor
Cursor
 
MySQL Training
MySQL TrainingMySQL Training
MySQL Training
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Test program
Test programTest program
Test program
 
How to handle Dynamic Objects with OpKey?
How to handle Dynamic Objects with OpKey?How to handle Dynamic Objects with OpKey?
How to handle Dynamic Objects with OpKey?
 
Modify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutletModify the bouncing ball example demonstrated/tutorialoutlet
Modify the bouncing ball example demonstrated/tutorialoutlet
 
Async Testing giving you a sinking feeling
Async Testing giving you a sinking feelingAsync Testing giving you a sinking feeling
Async Testing giving you a sinking feeling
 
Redux in Angular2 for jsbe
Redux in Angular2 for jsbeRedux in Angular2 for jsbe
Redux in Angular2 for jsbe
 
Testing with Kotlin
Testing with KotlinTesting with Kotlin
Testing with Kotlin
 
The Ring programming language version 1.10 book - Part 96 of 212
The Ring programming language version 1.10 book - Part 96 of 212The Ring programming language version 1.10 book - Part 96 of 212
The Ring programming language version 1.10 book - Part 96 of 212
 
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With FunctionsMS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
 
R: Apply Functions
R: Apply FunctionsR: Apply Functions
R: Apply Functions
 
Evolution of asynchrony in (ASP).NET
Evolution of asynchrony in (ASP).NETEvolution of asynchrony in (ASP).NET
Evolution of asynchrony in (ASP).NET
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java script
 

Similar to Data binding in AngularJS, from model to view

Scope demystified - AngularJS
Scope demystified - AngularJSScope demystified - AngularJS
Scope demystified - AngularJSSumanth krishna
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
 
Dive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceDive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceOleksii Prohonnyi
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of SignalsCoding Academy
 
Angular.js is super cool
Angular.js is super coolAngular.js is super cool
Angular.js is super coolMaksym Hopei
 
Containers & Dependency in Ember.js
Containers & Dependency in Ember.jsContainers & Dependency in Ember.js
Containers & Dependency in Ember.jsMatthew Beale
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications Juliana Lucena
 
AngularJS, More Than Directives !
AngularJS, More Than Directives !AngularJS, More Than Directives !
AngularJS, More Than Directives !Gaurav Behere
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails AppsRabble .
 
Scope.js prsentation
Scope.js prsentationScope.js prsentation
Scope.js prsentationAtishay Baid
 
Odtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for youOdtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for youLuc Bors
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014hezamu
 
Knockout mvvm-m2-slides
Knockout mvvm-m2-slidesKnockout mvvm-m2-slides
Knockout mvvm-m2-slidesMasterCode.vn
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 SlidesYarikS
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep diveAxilis
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.jsBert Wijnants
 
From Big to Massive – Scalability in AngularJS Applications
From Big to Massive – Scalability in AngularJS ApplicationsFrom Big to Massive – Scalability in AngularJS Applications
From Big to Massive – Scalability in AngularJS ApplicationsSebastian Fröstl
 

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

Scope demystified - AngularJS
Scope demystified - AngularJSScope demystified - AngularJS
Scope demystified - AngularJS
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
Timedobserver
TimedobserverTimedobserver
Timedobserver
 
Dive into Angular, part 3: Performance
Dive into Angular, part 3: PerformanceDive into Angular, part 3: Performance
Dive into Angular, part 3: Performance
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Angular.js is super cool
Angular.js is super coolAngular.js is super cool
Angular.js is super cool
 
Containers & Dependency in Ember.js
Containers & Dependency in Ember.jsContainers & Dependency in Ember.js
Containers & Dependency in Ember.js
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications
 
AngularJS, More Than Directives !
AngularJS, More Than Directives !AngularJS, More Than Directives !
AngularJS, More Than Directives !
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
Scope.js prsentation
Scope.js prsentationScope.js prsentation
Scope.js prsentation
 
Odtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for youOdtug2011 adf developers make the database work for you
Odtug2011 adf developers make the database work for you
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014
 
Knockout mvvm-m2-slides
Knockout mvvm-m2-slidesKnockout mvvm-m2-slides
Knockout mvvm-m2-slides
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
From Big to Massive – Scalability in AngularJS Applications
From Big to Massive – Scalability in AngularJS ApplicationsFrom Big to Massive – Scalability in AngularJS Applications
From Big to Massive – Scalability in AngularJS Applications
 

Recently uploaded

Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoilmeghakumariji156
 
Call girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsCall girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsMonica Sydney
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样ayvbos
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge GraphsEleniIlkou
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdfMatthew Sinclair
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样ayvbos
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查ydyuyu
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsMonica Sydney
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfJOHNBEBONYAP1
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsMonica Sydney
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.krishnachandrapal52
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrHenryBriggs2
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理F
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理F
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...gajnagarg
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查ydyuyu
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdfMatthew Sinclair
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsMonica Sydney
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查ydyuyu
 

Recently uploaded (20)

Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 
Call girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsCall girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girls
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 

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