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

Commit

Permalink
fix(ngInclude): do not compile template if original scope is destroyed
Browse files Browse the repository at this point in the history
With slow internet connection scope may be destroyed before template is loaded.
Previously in this case ngInclude compiled template that leaded to memory leaks
and errors in some cases.

Closes: #13515
Closes: #13543
  • Loading branch information
zgmnkv authored and gkalpak committed Dec 21, 2015
1 parent 689c01f commit 9590bcf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ng/directive/ngInclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ var ngIncludeDirective = ['$templateRequest', '$anchorScroll', '$animate',
//set the 2nd param to true to ignore the template request error so that the inner
//contents and scope can be cleaned up.
$templateRequest(src, true).then(function(response) {
if (scope.$$destroyed) return;

if (thisChangeId !== changeCounter) return;
var newScope = scope.$new();
ctrl.template = response;
Expand All @@ -253,6 +255,8 @@ var ngIncludeDirective = ['$templateRequest', '$anchorScroll', '$animate',
currentScope.$emit('$includeContentLoaded', src);
scope.$eval(onloadExp);
}, function() {
if (scope.$$destroyed) return;

if (thisChangeId === changeCounter) {
cleanupLastIncludeContent();
scope.$emit('$includeContentError', src);
Expand Down
20 changes: 20 additions & 0 deletions test/ng/directive/ngIncludeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,26 @@ describe('ngInclude', function() {
});


it('should not compile template if original scope is destroyed', function() {
module(function($provide) {
$provide.decorator('$compile', function($delegate) {
return jasmine.createSpy('$compile').andCallFake($delegate);
});
});
inject(function($rootScope, $httpBackend, $compile) {
$httpBackend.when('GET', 'url').respond('template text');
$rootScope.show = true;
element = $compile('<div ng-if="show"><div ng-include="\'url\'"></div></div>')($rootScope);
$rootScope.$digest();
$rootScope.show = false;
$rootScope.$digest();
$compile.reset();
$httpBackend.flush();
expect($compile).not.toHaveBeenCalled();
});
});


describe('autoscroll', function() {
var autoScrollSpy;

Expand Down

0 comments on commit 9590bcf

Please sign in to comment.