Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
Fixes #2 by not relying on finding an injector on the body.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Sep 9, 2014
1 parent c01cd76 commit d4b21f5
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions ng-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
var lastWatchCount = getWatcherCount() || 0;
var lastDigestLength = 0;

var bodyEl = angular.element(document.body);
var $rootScope;

var digestIsHijacked = false;

var listeners = {
Expand All @@ -36,7 +37,7 @@
return;
}
digestIsHijacked = true;
var $rootScope = bodyEl.injector().get('$rootScope');
var $rootScope = getRootScope();
var scopePrototype = Object.getPrototypeOf($rootScope);
var oldDigest = scopePrototype.$digest;
scopePrototype.$digest = function $digest() {
Expand All @@ -54,7 +55,7 @@
}

function autoload(options) {
if (window.angular && angular.element(document.body).injector()) {
if (window.angular && getRootScope()) {
showAngularStats(options);
} else {
// wait for angular to load...
Expand Down Expand Up @@ -97,6 +98,7 @@
}

// general variables
var bodyEl = angular.element(document.body);
var noDigestSteps = 0;

// add the DOM element
Expand Down Expand Up @@ -170,7 +172,6 @@

// start everything
shiftLeft();
var $rootScope = bodyEl.injector().get('$rootScope');
if(!$rootScope.$$phase) {
$rootScope.$digest();
}
Expand Down Expand Up @@ -262,6 +263,24 @@

// UTILITY FUNCTIONS

function getRootScope() {
if ($rootScope) {
return $rootScope;
}
var scopeEl = document.querySelector('.ng-scope');
if (!scopeEl) {
return null;
}

var injector = angular.element(scopeEl).injector();
if (!injector) {
return null;
}

$rootScope = injector.get('$rootScope');
return $rootScope;
}

// Uses timeouts to ensure that this is only run every 300ms (it's a perf bottleneck)
function getWatcherCount() {
window.clearTimeout(watchCountTimeout);
Expand Down

0 comments on commit d4b21f5

Please sign in to comment.