From 20bbb172b96e76b12836458d4a7bb06d48c59335 Mon Sep 17 00:00:00 2001 From: CodySchaaf Date: Thu, 21 Dec 2017 15:26:21 -0800 Subject: [PATCH] fix(ngMessages): fix orphaned ng-message comments when using ng-if Fix an issue where if you remove ng-message comments with ng-if before they are attached to the dom you will get an error when ngMessages tries to reveal them. --- src/ngMessages/messages.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ngMessages/messages.js b/src/ngMessages/messages.js index b6fbf622cd19..f2762d382497 100644 --- a/src/ngMessages/messages.js +++ b/src/ngMessages/messages.js @@ -663,6 +663,14 @@ function ngMessageDirectiveFactory() { require: '^^ngMessages', link: function(scope, element, attrs, ngMessagesCtrl, $transclude) { var commentNode = element[0]; + scope.$on('$destroy', function() { + // in the event that the element or a parent element is destroyed + // by another structural directive then it's time + // to deregister the message from the controller + // we do this here instead of in the attach function's destroy because otherwise + // comments that are not rendered will not be cleaned up + ngMessagesCtrl.deregister(commentNode); + }); var records; var staticExp = attrs.ngMessage || attrs.when; @@ -698,12 +706,8 @@ function ngMessageDirectiveFactory() { // when we are destroying the node later. var $$attachId = currentElement.$$attachId = ngMessagesCtrl.getAttachId(); - // in the event that the element or a parent element is destroyed - // by another structural directive then it's time - // to deregister the message from the controller currentElement.on('$destroy', function() { if (currentElement && currentElement.$$attachId === $$attachId) { - ngMessagesCtrl.deregister(commentNode); messageCtrl.detach(); } newScope.$destroy();