- Switching to Angular(Third Edition)
- Minko Gechev
- 452字
- 2021-07-02 15:23:34
Enhancing AngularJS's change detection
Now, let's take a step back and again think about the change detection mechanism of the framework.
We said that inside the digest loop, Angular evaluates registered expressions and compares the evaluated values with the values associated with the same expressions in the previous iteration of the loop.
The most optimal algorithm used for the comparison may differ depending on the type of the value returned from the evaluation of the expression. For instance, if we get a mutable list of items, we need to loop over the entire collection and compare the items in the collections one by one in order to verify if there is a change or not. However, if we have an immutable list, we can perform a check with a constant complexity, only by comparing references. This is the case because the instances of immutable data structures cannot change: instead of mutating the instance, we'll get a new reference with the modification applied.
In AngularJS, we can add watchers using a few methods. Two of them are $watch(exp, fn, deep) and $watchCollection(exp, fn). These methods give us some level of control over the way the change detection will perform the equality check. For example, adding a watcher using $watch and passing a false value as a third argument will make AngularJS perform a reference check (that is, compare the current value with the previous one using ===). However, if we pass a truthy (any true value), the check will be deep (that is, using angular.equals). This way, depending on the expected type of the returned by the expression value, we can add listeners in the most appropriate way in order to allow the framework to perform equality checks with the most optimal algorithm available. This API has two limitations:
- It does not allow you to choose the most appropriate equality check algorithm at runtime
- It does not allow you to extend the change detection to third parties for their specific data structures
The Angular core team assigned this responsibility to differs, allowing them to extend the change detection mechanism and optimize it, based on the data we use in our applications. Angular defines two base classes, which we can extend in order to define custom algorithms:
- KeyValueDiffer: This allows us to perform advanced diffing over key value-based data structures
- IterableDiffer: This allows us to perform advanced diffing over list-like data structures
Angular allows us to take full control over the change detection mechanism by extending it with custom algorithms or configuring it appropriately, which wasn't possible in AngularJS. We'll take a further look into the change detection and how we can configure it in Chapter 5, Getting Started with Angular Components and Directives.
- scikit-learn Cookbook
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- LabVIEW Graphical Programming Cookbook
- MySQL 8 DBA基礎(chǔ)教程
- Apache Spark 2 for Beginners
- 看透JavaScript:原理、方法與實(shí)踐
- 從0到1:HTML+CSS快速上手
- 深入理解Java7:核心技術(shù)與最佳實(shí)踐
- Visual C++開(kāi)發(fā)入行真功夫
- PHP+MySQL+Dreamweaver動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)從入門到精通(第3版)
- SQL Server數(shù)據(jù)庫(kù)管理與開(kāi)發(fā)兵書(shū)
- Python入門很輕松(微課超值版)
- Node.js 12實(shí)戰(zhàn)
- IPython Interactive Computing and Visualization Cookbook