简体   繁体   English

Angular如何在$摘要周期后更新视图?

[英]How does Angular update the view after a $digest cycle?

Does it: 可以:

1) Run through the DOM (starting at ng-app ) and if it sees a DOM node with a directive, update that DOM node with the corresponding model value. 1)运行DOM(从ng-app ),如果它看到带有指令的DOM节点,则使用相应的模型值更新该DOM节点。

or 要么

2) In running through the $$watchers list, if it detects a change, it has a reference to the DOM node it needs to update, so it just does that (so rather than running through the whole DOM, using this approach, it'd store and use a reference to a node in $$watchers ). 2)在运行$$watchers列表时,如果它检测到更改,它会引用它需要更新的DOM节点,所以它只是这样做(所以不要运行整个DOM,使用这种方法,它'存储并使用对$$watchers的节点的引用)。

It's more of the second approach. 它更像是第二种方法。

Angular does all it's heavy lifting via directives. Angular通过指令完成所有繁重工作。 Pretty much everything you use in Angular is a directive: 你在Angular中使用的几乎所有内容都是一个指令:

<div ng-app>
<div ng-controller>
<button ng-click>

<!-- input is actually a directive -->
<input ng-model="foo" />

All those little directives get a reference to the DOM element they are attached to, as well as a $scope object. 所有这些小指令都会获得对它们所附加的DOM元素的引用,以及$scope对象。 The directives simply register a callback to be executed when something changes. 指令只是注册一个回调,以便在发生变化时执行。

As you have already noted, these are called watchers. 正如您已经指出的那样,这些被称为观察者。

Scope is heirarchical, so there is always a tree of related objects that make up your application state. 范围是层次结构,因此始终存在构成应用程序状态的相关对象树。 When a $digest loop kicks off, it recursively walks that tree looking for changes, and firing off the callbacks (aka watchers) . 当一个$digest循环开始时,它递归地遍历那棵树寻找变化,然后解雇回调(又称观察者)

The callback can then choose to do whatever the heck it wants. 然后回调可以选择做任何他想要的事情。 It's just that in most cases, it's updating the DOM. 只是在大多数情况下,它正在更新DOM。

At the end of the day there really is nothing magical about how it happens. 在一天结束时,真的没有什么神奇的事情发生。 Just a structure of callbacks, that get executed when something changes. 只是一个回调结构,当事情发生变化时会被执行。

Here is a silly example of a custom directive that registers a watcher and updates the DOM whenever some property changes. 这是一个自定义指令的愚蠢示例,它注册观察者并在某些属性发生更改时更新DOM。

 (function(){ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } function updateValue(){ return { restrict:'A', link: function(scope, elem, attrs){ elem.on('click', function(){ //You would never actually do this, but // it's a demo scope[attrs.updateValue] = "rgb(" + getRandomInt(0, 255) + "," + getRandomInt(0, 255) + "," + getRandomInt(0, 255) + ")"; //kick off a digest loop manually // this is similar to what a lot of angular // directives do. scope.$digest(); }); } }; } function sillyDomChangeOn(){ return { restrict:'A', link: function(scope, elem, attrs){ scope.$watch(attrs.sillyDomChangeOn, function(newVal, oldVal){ elem.css('color', newVal); }); } }; } angular.module('my-app', []) .directive('updateValue', updateValue) .directive('sillyDomChangeOn', sillyDomChangeOn); }()); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"> <div ng-app="my-app" class="container"> <button type="button" class="btn btn-primary" update-value="randomVal">Update Value</button> <h3>Value: <code>{{randomVal}}</code></h3> <div class="well" silly-dom-change-on="randomVal"> <h3>This is just a random DIV</h3> <div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM