删除并从消化周期恢复范围(Remove and restore Scope from digest

2019-08-16 16:27发布

Is there a way to remove a scope from the digest cycles? In other words, to suspend/resume a scope digest cycle?

In my case, I have all pages already loaded, but not all of them visible. So I'd like to suspend the ones that aren't visible to avoid useless processing. I don't want to use ng-view + $route, I don't want/need deep-linking.

I saw this thread and arrived to this fiddle. It probably does the work, but it's pretty invasive and not very framework-update-friendly.

Is there any other solution like a $scope.suspend() and scope.resume()? Or a less invasive one (from framework perspective)? I'm currently thinking about $destroy and $compile cycles.

Answer 1:

我碰到同样的问题,我发现,不干预(太多)一个有趣的解决方案与AngularJS。 此添加要禁用范围:

var watchers;

scope.$on('suspend', function () {
  watchers = scope.$$watchers;
  scope.$$watchers = [];
});

scope.$on('resume', function () {
  scope.$$watchers = watchers;
  watchers = null;
});

然后,您可以禁用范围和它的孩子们: scope.$broadcast('suspend')并把它带回scope.$broadcast('resume')



Answer 2:

由于框架代表今天有没有方法来暂停/继续消化上一个范围。 说到这里有一个可以用来限制作为一个消化周期的一部分执行的手表数量的技术。

首先,如果屏幕的部分被隐藏无论如何,你可以使用纳克开关指令,从而从DOM中完全去除看不见的地方的家庭。

其次,如果一个消化周期从您的指令通过触发$apply ,并要限制手表重新评估,以子作用域你可以调用$digest ,而不是$apply

然后,是的,一个可能破坏并为你链接到讨论中所描述重新创建范围。 但是,如果你已经隐藏了DOM的部分,它听起来就像ng-switch可能是一个更好的选择。



文章来源: Remove and restore Scope from digest cycles