默认情况下,Angular 中的 $scope.$$watchers 中添加了什么?什么触发了 $digests? [英] What gets added to $scope.$$watchers by default in Angular? And what triggers $digests?
问题描述
我正在阅读 构建您自己的 AngularJS 并且对如何$scopes
, $watch
和 $digest
工作.我了解当您添加自己的 $watches
并调用自己的 $digests
时它是如何工作的.但是,我对默认情况下究竟发生了什么感到困惑.
I'm reading Build Your Own AngularJS and have a decent understanding of how $scopes
, $watch
and $digest
work. I understand how it works when you add your own $watches
and and call your own $digests
. However, I'm confused about what exactly is happening by default.
默认情况下,
$scope.$$watchers
会添加什么?你放在$scope
上的所有东西?您为ng-model
分配的所有内容?两个都?还有什么?
What gets added to
$scope.$$watchers
by default? Everything you put on$scope
? Everything you assign anng-model
to? Both? Something else?
$digests
什么时候被默认触发?输入字段更改?使用 ng-models
输入字段?其他?
And when exactly do $digests
get triggered by default? Input field changes? Input fields with ng-models
? Other?
推荐答案
一些使用 $watch
/$watchCollection
/$watchGroup<的常见指令/code> 内部:
Some of the common directives that use $watch
/ $watchCollection
/ $watchGroup
internally:
- ng-model
- ng-bind/{{ }}
- ng-show &ng隐藏
- ng-class
- ng-repeat
- ng-if
- ng-switch
- ng-include
请注意,唯一设置双向绑定的是 ng-model
(scope -> view & view -> scope).
Note that the only one that sets up a two-way binding is ng-model
(scope -> view & view -> scope).
其他人设置了单向绑定(范围 -> 视图).
The others set up a one-way binding (scope -> view).
简单地将某些内容暴露在例如控制器的 $scope
上将不会添加观察者.
Simply exposing something on for example a controller´s $scope
will not add a watcher.
例如,以下不会导致添加观察者:
For example, the following will not result in a watcher being added:
angular.module('myApp', []).controller('Controller', function MyCtrl($scope) {
$scope.value = 1;
});
一起:
<body ng-app="myApp" ng-controller="Controller">
</body>
但是如果你用以下的 HTML 替换,将会添加一个观察者:
But if you replace the HTML with the following one watcher will be added:
<body ng-app="myApp" ng-controller="Controller">
<div>{{value}}</div>
</body>
<小时>
触发摘要循环时的一些常见场景:
Some common scenarios when the digest cycle is triggered:
- 当
ng-click
被评估时 - 当
ng-model
改变时(例如在输入时) - 通过
$http
服务 - 在
$timeout
和$interval
- When
ng-click
is evaluated - When
ng-model
changes (for example when typing in an input) - By the
$http
service - In
$timeout
and$interval
<小时>
请注意,$apply
和 $digest
之间有一个很大的区别:
Note that there is one big difference between $apply
and $digest
:
调用 scope.$digest()
将仅在该作用域及其子作用域上执行观察者.
Calling scope.$digest()
will execute the watchers only on that scope and its children.
调用 scope.$apply()
将在 $rootScope
上触发 $digest
,这意味着将遍历所有范围并且所有观察者被处决.
Calling scope.$apply()
will trigger $digest
on the $rootScope
, which means all the scopes will be traversed and all watchers executed.
$apply
也接受一个表达式作为参数.该表达式将在 try-catch 语句中进行评估,任何异常都将传递给 $exceptionHandler
服务.
$apply
also accepts an expression as an argument. This expression will be evaluated inside a try-catch statement and any exception will be passed on to the $exceptionHandler
service.
$digest
不接受任何参数.
通常,当您追求微优化并且真正知道自己在做什么时,您只会调用 $digest
而不是 $apply
.
Usually you only call $digest
instead of $apply
when you are chasing micro optimizations and really know what you are doing.
这篇关于默认情况下,Angular 中的 $scope.$$watchers 中添加了什么?什么触发了 $digests?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!