diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index b00fe805..72fdc920 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -222,7 +222,15 @@ function $RootScopeProvider(){ } else { parent.$$childHead = parent.$$childTail = child; } + + // The listener needs to be added after the parent is set + if (isolate || parent != this) child.$on('$destroy', destroyChild); + return child; + + function destroyChild() { + child.$$destroyed = true; + } }, /** diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index ddde992a..f7bb09b0 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -4971,17 +4971,17 @@ describe('$compile', function() { return [$rootScope].concat( getChildScopes($rootScope) ).length; + } - function getChildScopes(scope) { - var children = []; - if (!scope.$$childHead) { return children; } - var childScope = scope.$$childHead; - do { - children.push(childScope); - children = children.concat(getChildScopes(childScope)); - } while ((childScope = childScope.$$nextSibling)); - return children; - } + function getChildScopes(scope) { + var children = []; + if (!scope.$$childHead) { return children; } + var childScope = scope.$$childHead; + do { + children.push(childScope); + children = children.concat(getChildScopes(childScope)); + } while ((childScope = childScope.$$nextSibling)); + return children; } beforeEach(module(function() { @@ -5117,6 +5117,23 @@ describe('$compile', function() { expect(countScopes($rootScope)).toEqual(1); })); + it('should destroy mark as destroyed all sub scopes of the scope being destroyed', + inject(function($compile, $rootScope) { + + element = $compile( + '
' + + '
{{ msg }}
' + + '
' + )($rootScope); + + $rootScope.$apply('t = true'); + var childScopes = getChildScopes($rootScope); + + $rootScope.$apply('t = false'); + for (var i = 0; i < childScopes.length; ++i) { + expect(childScopes[i].$$destroyed).toBe(true); + } + })); }); diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index 48c00148..5034ce63 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -1020,6 +1020,13 @@ describe('Scope', function() { expect(log).toBe('123'); })); + it('should broadcast the $destroy only once', inject(function($rootScope, log) { + var isolateScope = first.$new(true); + isolateScope.$on('$destroy', log.fn('event')); + first.$destroy(); + isolateScope.$destroy(); + expect(log).toEqual('event'); + })); it('should decrement ancestor $$listenerCount entries', inject(function($rootScope) { var EVENT = 'fooEvent',