Skip to content

Instantly share code, notes, and snippets.

@dhoko
Created September 22, 2015 09:42
Show Gist options
  • Save dhoko/cbd9a5c35d12740abeb4 to your computer and use it in GitHub Desktop.
Save dhoko/cbd9a5c35d12740abeb4 to your computer and use it in GitHub Desktop.
[Bookmarklet] Debug AngularJS
// Version without jQuery
javascript:(function () { var root = angular.element(document.getElementsByTagName('body')); var watchers = []; var f = function (element) { angular.forEach(['$scope', '$isolateScope'], function (scopeProperty) { if (element.data() && element.data().hasOwnProperty(scopeProperty)) { angular.forEach(element.data()[scopeProperty].$$watchers, function (watcher) { watchers.push(watcher); }); } }); angular.forEach(element.children(), function (childElement) { f(angular.element(childElement)); }); }; f(root); var watchersWithoutDuplicates = []; angular.forEach(watchers, function(item) { if(watchersWithoutDuplicates.indexOf(item) < 0) { watchersWithoutDuplicates.push(item); } }); console.log(watchersWithoutDuplicates.length); })();
// Version with jQuery
javascript:(function () { var root = $(document.getElementsByTagName('body')); var watchers = []; var f = function (element) { if (element.data().hasOwnProperty('$scope')) { angular.forEach(element.data().$scope.$$watchers, function (watcher) { watchers.push(watcher); }); } angular.forEach(element.children(), function (childElement) { f($(childElement)); }); }; f(root); console.log(watchers.length); })();
/**
* Usage
* Click bookmarklet
* In da console view total
*/
// Get the current scope for dom item
javascript:scope=function(o){console.log(angular.element(o).scope()); return angular.element(o).scope()}
/**
* Usage
* clcik bookmarklet
* select item in the dom
* in da console do scope($0) ($0 or document.querySelector(<selector>))
// Inject any service from your app
javascript:inject = angular.element(document.body).injector().get
/**
* Usage
* Click bookmarklet
* In da console do var service = inject(<serviceName>);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment