Joined August 2018
·

Medhi

Softwae Engineer
·
Wisconsin
·

Posted to Speeding up AngularJS's $digest loop over 1 year ago

Would this directive work to suspend watchers while the component is hidden by ng-show

directive("suspendWatchers", function() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(attrs.ngShow, function(newval) {
toggleWatchers(scope, false, newval, this);

            function toggleWatchers(scope, sibling, pause, keep) {
              if (pause) {
               if (scope.watchers_bckp) {
               scope.$$watchers = scope.watchers_bckp; 
               scope.watchers_bckp= [];
                }   
              } else {
                scope.watchers_bckp = scope.$$watchers;
                scope.$$watchers = []; 
                scope.$$watchers.push(keep);
             }

             if (scope.$$childHead)    
                toggleWatchers(scope.$$childHead, true, pause); 

              if (scope.$$nextSibling && sibling)
                toggleWatchers(scope.$$nextSibling, true, pause);           
           }
      })
    }
  }
})
Achievements
1 Karma
0 Total ProTip Views
Interests & Skills