Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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
You can’t perform that action at this time.