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

Commit 69d9072

Browse files
jpekkalapetebacondarwin
authored andcommitted
fix(ngMessagesInclude): do not compile template if scope is destroyed
Messages imported with `ngMessagesInclude` are loaded asynchronously and they can arrive after the ngMessages element has already been removed from DOM. Previously we tried to compile these messages and that caused a `$compile:ctreq` error. Now they are silently ignored if `ngMessagesInclude`'s scope has already been destroyed. Closes #12695 Closes #14640
1 parent 9b5de0a commit 69d9072

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/ngMessages/messages.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ angular.module('ngMessages', [])
550550
link: function($scope, element, attrs) {
551551
var src = attrs.ngMessagesInclude || attrs.src;
552552
$templateRequest(src).then(function(html) {
553+
if ($scope.$$destroyed) return;
554+
553555
$compile(html)($scope, function(contents) {
554556
element.after(contents);
555557

test/ngMessages/messagesSpec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,25 @@ describe('ngMessages', function() {
842842
})
843843
);
844844

845+
it('should not throw if scope has been destroyed when template request is ready',
846+
inject(function($rootScope, $httpBackend, $compile) {
847+
$httpBackend.expectGET('messages.html').respond('<div ng-message="a">A</div>');
848+
$rootScope.show = true;
849+
var html =
850+
'<div ng-if="show">' +
851+
'<div ng-messages="items">' +
852+
'<div ng-messages-include="messages.html"></div>' +
853+
'</div>' +
854+
'</div>';
855+
856+
element = $compile(html)($rootScope);
857+
$rootScope.$digest();
858+
$rootScope.show = false;
859+
$rootScope.$digest();
860+
expect(function() {
861+
$httpBackend.flush();
862+
}).not.toThrow();
863+
}));
845864
});
846865

847866
describe('when multiple', function() {

0 commit comments

Comments
 (0)