本文介绍了角JS:$ Scope.Apply()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道更多关于$ scope.apply()在实时使用。
多少次我们在控制器scope.apply()使用$?

I want to know more about $scope.apply() in real time usage.How many times can we use $scope.apply() in a controller?

例如,我有这样NG-点击(),NG-变化(),NG-模糊()等等。所有的活动都在同一个控制器中的一些事件。对于每一个事件,我应该使用$ scope.apply()?如果是的话,我得到错误:

For example, I have some events like ng-click() , ng-change(), ng-blur() etc. All events are in the same controller. For each and every event, should i use $scope.apply()? If yes, I am getting error :

Error: [$rootScope:inprog] [http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply][1]
at Error (native)

我在这个论坛已阅读,取消除$ scope.apply()会解决这个问题。

I have read in this forum that removing the addition $scope.apply() will resolve the issue.

angularjs 。$ $范围适用于()给出了这样的错误:错误:[$ rootScope:inprog]

我实现了删除多个$ scope.apply()从code的同一个解决方案。该errror走了,但我想知道如何和为什么?

I implemented the same solution of removing multiple $scope.apply() from the code. The errror is gone, but I want to know how and why?

任何人都可以请解释一下。

Can anyone please explain.

在此先感谢。

推荐答案

$ scope.apply()是更新的DOM触发,并且在大多数情况下(如从DOM如 NG-点击触发器的评价是包裹在一个 $ scope.apply(),因为它被传递到控制器,你一般不需要调用 $ scope.apply(),因为它已经被处理,但如果您有问题事情没有更新正确,则可以使用 $ scope.apply()基本上轻推它更新,为prevent调用应用时应用已经被评估你可以做一个安全的检查,像这样:

$scope.apply() is a trigger to update the DOM, and in most cases (such as a trigger from the DOM like ng-click the evaluation of the trigger is wrapped in a $scope.apply() as it is passed to your controller. You generally don't need to call $scope.apply() as it is already being handled, but if you are having issues with something not updating correctly, you can use $scope.apply() to basically nudge it to update. In order to prevent calling apply when apply is already being evaluated you can do a safe check like so:

if (!$scope.$$phase)
    $scope.apply();

$$阶段是一个角内部属性时,没有适用的范围是在建即空/未定义,并具有价值,当 $ scope.apply()正在执行。

The $$phase is an angular internal property that is null/undefined when no scope apply is in progress, and has a value when a $scope.apply() is executing.

$ scope.apply()的工作原理是找到最子女范围(最内侧的范围),并检查的变化,要求手表等,那么它爬了范围直到到达根范围内,所以你可以想象这是一个相当沉重的呼叫,应尽量避免。

$scope.apply() works by finding the most child scope (the innermost scope) and checking for changes, calling watches, etc, then it crawls up the scopes until it gets to the root scope, so as you might imagine it is a rather heavy call and should be avoided when possible.

这篇关于角JS:$ Scope.Apply()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:36