得到什么在默认情况下角加入到$范围。$$观察?什么触发$摘要? [英] What gets added to $scope.$$watchers by default in Angular? And what triggers $digests?

查看:126
本文介绍了得到什么在默认情况下角加入到$范围。$$观察?什么触发$摘要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读自创AngularJS 以及对如何体面的理解 $范围 $观看 $消化的工作。我理解它是如何工作的,当你添加自己的 $手表键,并呼吁自己的 $摘要。不过,我感到困惑,究竟是默认发生的


  1. 什么被添加到 $范围。$$观察家默认?你把 $范围一切吗?一切你分配一个 NG-模型来?都?别的东西吗?


  2. 和确切时间做 $摘要默认情况下被触发?输入领域的变化?与 NG-模型输入域?其他?



解决方案

一些使用通用指令 $观看 / $ watchCollection / $ watchGroup 内部:


  1. NG-模型

  2. NG-绑定/ {{}}

  3. NG-秀&放大器; NG-隐藏

  4. 纳克级

  5. NG-重复

  6. NG-如果

  7. NG-开关

  8. NG-包括

需要注意的是建立一个双向绑定的只有一个是 NG-模型(范围 - >查看&放大器;查看 - >范围内)。

其他人建立了一个单向绑定(范围 - >视图)。

只要暴露在例如控制器的 $范围的东西会的不可以添加一个观察者。

例如,下面将不会导致观察者被添加

  angular.module('对myApp',[])。控制器('控制',功能MyCtrl($范围){
  $ scope.value = 1;
});

连同

 <机身NG-应用=对myAppNG控制器=控制器>
< /身体GT;

但是,如果你用下面的一个观察者取代HTML将被添加:

 <机身NG-应用=对myAppNG控制器=控制器>
  < D​​IV> {{}值}< / DIV>
< /身体GT;


在消化周期触发一些常见的场景:


  1. NG-点击评估

  2. NG-模型的变化(例如在输入打字的时候)

  3. $ HTTP 服务

  4. $超时 $间隔


请注意,有之间的一个很大的区别 $适用 $消化

调用范围。$摘要()将执行只在范围和其子的观察家。

调用范围。$适用()将触发 $消化 $ 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.

  1. What gets added to $scope.$$watchers by default? Everything you put on $scope? Everything you assign an ng-model to? Both? Something else?

  2. And when exactly do $digests get triggered by default? Input field changes? Input fields with ng-models? Other?

解决方案

Some of the common directives that use $watch / $watchCollection / $watchGroup internally:

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

  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


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屋!

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