Remove and restore Scope from digest cycles

后端 未结 2 1442
忘掉有多难
忘掉有多难 2021-02-19 21:37

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

2条回答
  •  孤街浪徒
    2021-02-19 22:15

    I've ran into the same problem and I found an interesting solution that doesn't interfere (too much) with AngularJS. Add this to the scopes you want to disable:

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

    Then, you can disable a scope and its children with: scope.$broadcast('suspend') and bring it back with scope.$broadcast('resume').

提交回复
热议问题