Skip to content
Permalink
Browse files

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
  • Loading branch information
jpekkala authored and petebacondarwin committed May 22, 2016
1 parent e67c3f8 commit ab247d620345f513138a3149b8ef0653e5cd8328
Showing with 21 additions and 0 deletions.
  1. +2 −0 src/ngMessages/messages.js
  2. +19 −0 test/ngMessages/messagesSpec.js
@@ -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);

@@ -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() {

0 comments on commit ab247d6

Please sign in to comment.
You can’t perform that action at this time.