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( + '