Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

TRANSCLUDE MEMORY LEAK: fix($compile): attach handler to a real element #9129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

var clone = transcludeFn(transcludedScope, cloneFn, controllers, previousBoundTranscludeFn, futureParentElement);
if (scopeCreated && !elementTransclusion) {
clone.on('$destroy', function() { transcludedScope.$destroy(); });
futureParentElement.on('$destroy', function() { transcludedScope.$destroy(); });
}
return clone;
};
Expand Down
58 changes: 58 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5551,6 +5551,64 @@ describe('$compile', function() {
});

});

it('should not leak memory when a directive removes the element containing the transclude directive', function() {

module(function() {
directive('toggle', function() {
return {
transclude: true,
template:
'<section class="toggle">' +
'<div ng:if="t">' +
'<div ng:transclude></div>' +
'</div>' +
'</section>'
};
});
});

inject(function($compile, $rootScope) {

function getAllScopes() {
return [$rootScope].concat(
getChildScopes($rootScope)
);

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;
}
}

$rootScope.messages = ['message 0'];

element = $compile(
'<div toggle>' +
' <div ng:repeat="message in messages">{{ message }}</div>' +
'</div>'
)($rootScope);

$rootScope.$apply('t = true');
expect(element.text()).toContain('message 0');
expect(getAllScopes().length).toEqual(4);
$rootScope.$apply('t = false');
expect(element.text()).not.toContain('message 0');
expect(getAllScopes().length).toEqual(1);
$rootScope.$apply('t = true');
expect(element.text()).toContain('message 0');
expect(getAllScopes().length).toEqual(4);
$rootScope.$apply('t = false');
expect(element.text()).not.toContain('message 0');
expect(getAllScopes().length).toEqual(1);
});
});
});


Expand Down