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
This commit is contained in:
Jukka
2016-05-23 00:35:50 +03:00
committed by Peter Bacon Darwin
parent e67c3f8ab6
commit ab247d6203
2 changed files with 21 additions and 0 deletions
+2
View File
@@ -550,6 +550,8 @@ angular.module('ngMessages', [])
link: function($scope, element, attrs) {
var src = attrs.ngMessagesInclude || attrs.src;
$templateRequest(src).then(function(html) {
if ($scope.$$destroyed) return;
$compile(html)($scope, function(contents) {
element.after(contents);
+19
View File
@@ -842,6 +842,25 @@ describe('ngMessages', function() {
})
);
it('should not throw if scope has been destroyed when template request is ready',
inject(function($rootScope, $httpBackend, $compile) {
$httpBackend.expectGET('messages.html').respond('<div ng-message="a">A</div>');
$rootScope.show = true;
var html =
'<div ng-if="show">' +
'<div ng-messages="items">' +
'<div ng-messages-include="messages.html"></div>' +
'</div>' +
'</div>';
element = $compile(html)($rootScope);
$rootScope.$digest();
$rootScope.show = false;
$rootScope.$digest();
expect(function() {
$httpBackend.flush();
}).not.toThrow();
}));
});
describe('when multiple', function() {