默认情况下,Angular 中的 $scope.$$watchers 中添加了什么?什么触发了 $digests? [英] What gets added to $scope.$$watchers by default in Angular? And what triggers $digests?

查看:16
本文介绍了默认情况下,Angular 中的 $scope.$$watchers 中添加了什么?什么触发了 $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.

  1. 默认情况下,$scope.$$watchers 会添加什么?你放在 $scope 上的所有东西?您为 ng-model 分配的所有内容?两个都?还有什么?

  1. What gets added to $scope.$$watchers by default? Everything you put on $scope? Everything you assign an ng-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:

  1. ng-model
  2. ng-bind/{{ }}
  3. ng-show &ng隐藏
  4. ng-class
  5. ng-repeat
  6. ng-if
  7. ng-switch
  8. 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:

  1. ng-click 被评估时
  2. ng-model 改变时(例如在输入时)
  3. 通过$http 服务
  4. $timeout$interval
  1. When ng-click is evaluated
  2. When ng-model changes (for example when typing in an input)
  3. By the $http service
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆