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();