为什么范围。$适用()调用$ rootScope。$摘要(),而不是这个。$摘要()? [英] Why Scope.$apply() calls $rootScope.$digest() rather than this.$digest()?

查看:152
本文介绍了为什么范围。$适用()调用$ rootScope。$摘要(),而不是这个。$摘要()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AngularJS范围。$适用()调用每个事件处理程序和其他一些情况下(用于输入指令,对指令选择改变事件等的keydown /输入事件)。

In AngularJS scope.$apply() is called on every event handler (keydown/input events for input directive, change event for select directive, etc) and some other cases.

请参阅小例子。似乎ngRepeat被重新计算并重新绘制在每个KEYDOWN尽管发生在其他范围的变化的事实。

See small example. Seems that ngRepeat is recalculated and redrawn on every keydown despite the fact that changes occur in the other scope.

这将是有趣的,知道该决定的理由。

It would be interesting to know rationale for such decision.

推荐答案

将是巨大的,有这个AngularJS作者,但我相信,$摘要()的需要,因为在直放站能引起的变化上的$ rootScope被称为在其他领域的副作用(甚至是$ rootScope)。

Would be great to have AngularJS authors on this but I believe that the $digest() needs to be called on the $rootScope since changes triggered in a repeater's can have side-effects in other scopes (even the $rootScope).

问题是,方法子作用域触发可以影响父作用域对象(因为子作用域从父母继承的人)。所以,即使在子作用域定义函数不能从父范围修改对象的引用的他们仍然可以修改在父范围内定义的对象值。

The thing is that methods triggered in child scopes can influence objects in parent scopes (since child scopes inherit from parent ones). So, even if functions defined in child scopes can't modify object references from a parent scope they still can modify values in objects defined in a parent scope.

以上听起来有点神秘让我们考虑的项目列表的(有点做作)的例子:

The above might sound a bit cryptic so let's consider an (a bit artificial) example with a list of items:

$scope.items = [{name: 'foo', value:0}, {name: 'bar', value:0}, {name: 'baz', value:0}];

现在,让我们使用NG-重复显示上面的列表中,让我们说,点击一个项目应该增加值的其他的项目(再次,这个例子是有点做作,但是点这里是一个动作在一个范围内触发可以在其它范围内的副作用)。这可能是这样的:

Now, let's use ng-Repeat to display the above list and let's say that clicking on a item should increment a value of other items (once again, the example is a bit artificial but the point here is that an action triggered in one scope can have side effect in other scopes). It could go like this:

$scope.incOther = function(item) {
        for (var i=0; i<$scope.items.length; i++){
            if ($scope.items[i] !== item){
                $scope.items[i].value++;
            }
        }
    };

该示例函数将修改值在其它范围和AngularJS - 以显示正确的结果 - 需要评估父范围观察家(高达$ rootScope,因为我们不知道在哪里的对象定义)。

The example function would modify values in other scopes and AngularJS - to display correct results - need to evaluate watchers in parent scopes (up to the $rootScope since we don't know where an object was defined).

下面是完整的jsfiddle说明这一点:<一href=\"http://jsfiddle.net/pkozlowski_opensource/Z6e5g/3/\">http://jsfiddle.net/pkozlowski_opensource/Z6e5g/3/

Here is the complete jsFiddle illustrating this: http://jsfiddle.net/pkozlowski_opensource/Z6e5g/3/

其实上面的jsfiddle还包括在$ rootScope对象以表明观察家评价真正需要在最高层开始。

In fact the above jsFiddle also includes an object in a $rootScope to illustrate that the watchers evaluation really needs to start at the very top.

这篇关于为什么范围。$适用()调用$ rootScope。$摘要(),而不是这个。$摘要()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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