planzelle

angularJs watchers

Mar 9th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // from: https://angularjs.de/artikel/angularjs-access-scope-via-console
  2. // bug fixed
  3.  
  4. // fetch all elements with ng-scope class
  5. var scopeElements = Array.prototype.slice.apply(document.querySelectorAll(".ng-scope"));
  6. // map elements to scopes
  7. var scopes = scopeElements.map(function (element) {
  8.     return angular.element(element).scope();
  9. });
  10. // filter duplicated scopes created by ng-view
  11. var scopesById = {};
  12. var uniqueScopes = [];
  13. scopes.forEach(function (scope) {
  14.     if (scopesById[scope.$id] === undefined) {
  15.         scopesById[scope.$id] = scope;
  16.         uniqueScopes.push(scope);
  17.     }
  18. });
  19. // map uniqueScopes to watchers
  20. var watchers = uniqueScopes.map(function (scope) {
  21.     return scope.$$watchers;
  22. });
  23. // extract the length
  24. var watchersLengths = watchers.map(function (watcher) {
  25.     return watchers.length;
  26. })
  27. // sum up the length with reduce
  28. var watchersCount = watchersLengths.reduce(function(a,b) {
  29.     return a + b;
  30. });
  31. console.log("watchersCount: " + watchersCount);
Add Comment
Please, Sign In to add comment