官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 泗水县| 兰西县| 手机| 绥德县| 土默特左旗| 邻水| 永安市| 内乡县| 永康市| 古丈县| 宜宾市| 醴陵市| 余江县| 克东县| 厦门市| 西乌珠穆沁旗| 襄樊市| 开封市| 洪泽县| 马边| 玛沁县| 灵山县| 威海市| 黄骅市| 福海县| 夏河县| 渑池县| 萍乡市| 满城县| 洛川县| 周宁县| 吉木乃县| 息烽县| 富裕县| 余江县| 古丈县| 大英县| 玉田县| 休宁县| 牟定县| 忻城县|