得到什么在默认情况下角加入到$范围。$$观察?什么触发$摘要? [英] What gets added to $scope.$$watchers by default in Angular? And what triggers $digests?
问题描述
我在读自创AngularJS 以及对如何体面的理解 $范围
, $观看
和 $消化
的工作。我理解它是如何工作的,当你添加自己的 $手表
键,并呼吁自己的 $摘要
。不过,我感到困惑,究竟是默认发生的 的
-
什么被添加到
$范围。$$观察家
默认?你把$范围
一切吗?一切你分配一个NG-模型
来?都?别的东西吗? -
和确切时间做
$摘要
默认情况下被触发?输入领域的变化?与NG-模型输入域
?其他?
一些使用通用指令 $观看
/ $ watchCollection
/ $ watchGroup
内部:
- NG-模型
- NG-绑定/ {{}}
- NG-秀&放大器; NG-隐藏
- 纳克级
- NG-重复
- NG-如果
- NG-开关
- NG-包括
需要注意的是建立一个双向绑定的只有一个是 NG-模型
(范围 - >查看&放大器;查看 - >范围内)。
其他人建立了一个单向绑定(范围 - >视图)。
只要暴露在例如控制器的 $范围
的东西会的不可以添加一个观察者。
例如,下面将不会导致观察者被添加
angular.module('对myApp',[])。控制器('控制',功能MyCtrl($范围){
$ scope.value = 1;
});
连同
<机身NG-应用=对myAppNG控制器=控制器>
< /身体GT;
但是,如果你用下面的一个观察者取代HTML将被添加:
<机身NG-应用=对myAppNG控制器=控制器>
< DIV> {{}值}< / DIV>
< /身体GT;
在消化周期触发一些常见的场景:
- 当
NG-点击
评估 - 当
NG-模型
的变化(例如在输入打字的时候) - 在
$ HTTP
服务 - 在
$超时
和$间隔
请注意,有之间的一个很大的区别 $适用
和 $消化
:
调用范围。$摘要()
将执行只在范围和其子的观察家。
调用范围。$适用()
将触发 $消化
在 $ rootScope
,这意味着所有范围会遍历所有观察家执行。
$适用
还接受前pression作为参数。这当然pression将一个try-catch语句中进行评估,任何异常将被传递到 $ exceptionHandler的
服务。
$摘要
不接受任何参数。
通常你只能叫 $消化
而不是 $适用
当你追逐微的优化,真正知道你在做什么。
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.
What gets added to
$scope.$$watchers
by default? Everything you put on$scope
? Everything you assign anng-model
to? Both? Something else?And when exactly do
$digests
get triggered by default? Input field changes? Input fields withng-models
? Other?
Some of the common directives that use $watch
/ $watchCollection
/ $watchGroup
internally:
- ng-model
- ng-bind / {{ }}
- ng-show & ng-hide
- ng-class
- ng-repeat
- ng-if
- ng-switch
- ng-include
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).
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;
});
Together with:
<body ng-app="myApp" ng-controller="Controller">
</body>
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:
- When
ng-click
is evaluated - When
ng-model
changes (for example when typing in an input) - By the
$http
service - In
$timeout
and$interval
Note that there is one big difference between $apply
and $digest
:
Calling scope.$digest()
will execute the watchers only on that scope and its children.
Calling scope.$apply()
will trigger $digest
on the $rootScope
, which means all the scopes will be traversed and all watchers executed.
$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
does not accept any arguments.
Usually you only call $digest
instead of $apply
when you are chasing micro optimizations and really know what you are doing.
这篇关于得到什么在默认情况下角加入到$范围。$$观察?什么触发$摘要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!