diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 233dfa3b2f60..5ce7cdb32355 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -133,6 +133,7 @@ function $RootScopeProvider(){ this.$$asyncQueue = []; this.$$postDigestQueue = []; this.$$listeners = {}; + this.$$listenerCount = {}; this.$$isolateBindings = {}; } @@ -192,6 +193,7 @@ function $RootScopeProvider(){ } child['this'] = child; child.$$listeners = {}; + child.$$listenerCount = {}; child.$parent = this; child.$$watchers = child.$$nextSibling = child.$$childHead = child.$$childTail = null; child.$$prevSibling = this.$$childTail; @@ -696,6 +698,18 @@ function $RootScopeProvider(){ this.$$destroyed = true; if (this === $rootScope) return; + var self = this; + forEach(this.$$listenerCount, function(count, name) { + var current = self; + do { + current.$$listenerCount[name] -= count; + + if (current.$$listenerCount[name] === 0) { + delete current.$$listenerCount[name]; + } + } while ((current = current.$parent)); + }); + if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling; if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling; if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling; @@ -885,6 +899,14 @@ function $RootScopeProvider(){ } namedListeners.push(listener); + var current = this; + do { + if (!current.$$listenerCount[name]) { + current.$$listenerCount[name] = 0; + } + current.$$listenerCount[name]++; + } while ((current = current.$parent)); + return function() { namedListeners[indexOf(namedListeners, listener)] = null; }; @@ -998,8 +1020,7 @@ function $RootScopeProvider(){ listeners, i, length; //down while you can, then up and next sibling or up and next sibling until back at root - do { - current = next; + while ((current = next)) { event.currentScope = current; listeners = current.$$listeners[name] || []; for (i=0, length = listeners.length; i