本文介绍了如何调用函数时的角度观察周期或消化周期结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来调用自定义函数在角角完成后所有的观察周期。

Is there any way to call custom function in angular after angular finishes all watch cycle.

要求
我有我的控制器内的多个手表功能。现在我想只有在所有的手表功能由角执行,以执行一个功能

推荐答案

有几个方法可以做到注册回调一旦消化完毕。

There are couple of ways to do register a callback once a digest is completed.

$$ postDigest
$范围。$$ postDigest 当前 $消化周期完成后,触发回调。

Using $$postDigest:$scope.$$postDigest fires a callback after the current $digest cycle completed.

然而,这在未来消化周期后只运行一次。为了让每个运行周期消化它运行后,随着 $观看。这是基于这里给出的code样品

However this runs only once after the next digest cycle. To make it run after each digest cycle run it along with $watch. This is based on the code sample given here

var hasRegistered = false;
$scope.$watch(function() {
  if (hasRegistered) return;
  hasRegistered = true;
  $scope.$$postDigest(function() {
    hasRegistered = false;
    fn();
  });
});

$观看可在消化周期期间被多次触发,所以我们使用标志 hasRegistered 至prevent $$ postDigest 回调要注册多次。
注:$$ postDigest不会触发另一个摘要周期。因此,任何modifcation到 $范围 $$ postDigest 不会得到体现在DOM。 $ 表示这是angularjs私有功能,所以功能并不稳定,在未来可能会发生改变。

The $watch can get triggered multiple times during a digest cycle so we use a flag hasRegistered to prevent $$postDigest callback to be registered multiple times.Note: $$postDigest will not trigger another digest cycle. So any modifcation to $scope inside $$postDigest will not get reflected in the dom. $$ means this is a private function in angularjs, so the function is not stable and may change in the future.

$超时

Using $timeout:

$timeout(function(){
 console.log("Running after the digest cycle");
},0,false);

这之后运行当前消化周期完成。
注:第三个参数设置为false,以prevent另一摘要周期触发

This runs after the current digest cycle is complete.Note: The third argument is set to false to prevent another digest cycle trigger.

这篇关于如何调用函数时的角度观察周期或消化周期结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!