SHARE
TWEET

watchers

WhatEverEU Apr 5th, 2018 21 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getWatchers(root) {
  2.   root = angular.element(root || document.documentElement);
  3.   var watcherCount = 0;
  4.  
  5.   function getElemWatchers(element) {
  6.     var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
  7.     var scopeWatchers = getWatchersFromScope(element.data().$scope);
  8.     var watchers = scopeWatchers.concat(isolateWatchers);
  9.     angular.forEach(element.children(), function (childElement) {
  10.       watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
  11.     });
  12.     return watchers;
  13.   }
  14.  
  15.   function getWatchersFromScope(scope) {
  16.     if (scope) {
  17.       return scope.$$watchers || [];
  18.     } else {
  19.       return [];
  20.     }
  21.   }
  22.  
  23.   return getElemWatchers(root);
  24. }
  25. getWatchers().length
  26.  
  27. // get all watchers on the whole page
  28. getWatchers();
  29. // get watchers of a specific element (and its children)
  30. getWatchers(document.body);
  31. // select the element of interest in Chrome Dev tools
  32. getWatchers($0);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top