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

fix(ngInclude): do not compile template if original scope is destroyed #13543

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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this code path is not tested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is not tested. I think it is quite internal logic, the aim is not to perform unnecessary cleanup. By the time when scope is destroyed element should be also removed, so no need to call cleanupLastIncludeContent, and $emit on destroyed scope is useless. There are no changes visible from outside.
If test is required, I can try to spy on scope and element and check that scope.$destroy, scope.$emit and element.remove are not being called.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think there is a need for a lot of spying, you can have another test just like the one you have with these differences

  • same template but without the ng-if
  • creating a new scope from $rootScope, linking the template to it
  • registering a listener
  • destroying the scope
  • doing the http flush

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean listener to $includeContentError event? What should I expect in this case? this event will not fire anyway, because scope removes all listeners on destroy.
Or do you mean that we shouldn't make any expectations, just check that this code works?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right, and nothing else is needed.


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