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

Commit 9590bcf

Browse files
zgmnkvgkalpak
authored andcommitted
fix(ngInclude): do not compile template if original scope is destroyed
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
1 parent 689c01f commit 9590bcf

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/ng/directive/ngInclude.js

+4
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ var ngIncludeDirective = ['$templateRequest', '$anchorScroll', '$animate',
232232
//set the 2nd param to true to ignore the template request error so that the inner
233233
//contents and scope can be cleaned up.
234234
$templateRequest(src, true).then(function(response) {
235+
if (scope.$$destroyed) return;
236+
235237
if (thisChangeId !== changeCounter) return;
236238
var newScope = scope.$new();
237239
ctrl.template = response;
@@ -253,6 +255,8 @@ var ngIncludeDirective = ['$templateRequest', '$anchorScroll', '$animate',
253255
currentScope.$emit('$includeContentLoaded', src);
254256
scope.$eval(onloadExp);
255257
}, function() {
258+
if (scope.$$destroyed) return;
259+
256260
if (thisChangeId === changeCounter) {
257261
cleanupLastIncludeContent();
258262
scope.$emit('$includeContentError', src);

test/ng/directive/ngIncludeSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,26 @@ describe('ngInclude', function() {
398398
});
399399

400400

401+
it('should not compile template if original scope is destroyed', function() {
402+
module(function($provide) {
403+
$provide.decorator('$compile', function($delegate) {
404+
return jasmine.createSpy('$compile').andCallFake($delegate);
405+
});
406+
});
407+
inject(function($rootScope, $httpBackend, $compile) {
408+
$httpBackend.when('GET', 'url').respond('template text');
409+
$rootScope.show = true;
410+
element = $compile('<div ng-if="show"><div ng-include="\'url\'"></div></div>')($rootScope);
411+
$rootScope.$digest();
412+
$rootScope.show = false;
413+
$rootScope.$digest();
414+
$compile.reset();
415+
$httpBackend.flush();
416+
expect($compile).not.toHaveBeenCalled();
417+
});
418+
});
419+
420+
401421
describe('autoscroll', function() {
402422
var autoScrollSpy;
403423

0 commit comments

Comments
 (0)