@@ -417,13 +417,6 @@ angular.module('ngMessages', [], function initAngularHelpers() {
417417
418418 $scope . $watchCollection ( $attrs . ngMessages || $attrs [ 'for' ] , ctrl . render ) ;
419419
420- // If the element is destroyed, proactively destroy all the currently visible messages
421- $element . on ( '$destroy' , function ( ) {
422- forEach ( messages , function ( item ) {
423- item . message . detach ( ) ;
424- } ) ;
425- } ) ;
426-
427420 this . reRender = function ( ) {
428421 if ( ! renderLater ) {
429422 renderLater = true ;
@@ -498,6 +491,9 @@ angular.module('ngMessages', [], function initAngularHelpers() {
498491 function removeMessageNode ( parent , comment , key ) {
499492 var messageNode = messages [ key ] ;
500493
494+ // This message node may have already been removed by a call to deregister()
495+ if ( ! messageNode ) return ;
496+
501497 var match = findPreviousMessage ( parent , comment ) ;
502498 if ( match ) {
503499 match . next = messageNode . next ;
@@ -702,6 +698,8 @@ function ngMessageDirectiveFactory() {
702698 // by another structural directive then it's time
703699 // to deregister the message from the controller
704700 currentElement . on ( '$destroy' , function ( ) {
701+ // If the message element was removed via a call to `detach` then `currentElement` will be null
702+ // So this handler only handles cases where something else removed the message element.
705703 if ( currentElement && currentElement . $$attachId === $$attachId ) {
706704 ngMessagesCtrl . deregister ( commentNode ) ;
707705 messageCtrl . detach ( ) ;
@@ -719,6 +717,14 @@ function ngMessageDirectiveFactory() {
719717 }
720718 }
721719 } ) ;
720+
721+ // We need to ensure that this directive deregisters itself when it no longer exists
722+ // Normally this is done when the attached element is destroyed; but if this directive
723+ // gets removed before we attach the message to the DOM there is nothing to watch
724+ // in which case we must deregister when the containing scope is destroyed.
725+ scope . $on ( '$destroy' , function ( ) {
726+ ngMessagesCtrl . deregister ( commentNode ) ;
727+ } ) ;
722728 }
723729 } ;
724730 } ] ;
0 commit comments