Skip to content

Commit

Permalink
fix(ngMessages): fix orphaned ng-message comments when using ng-if
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CodySchaaf committed Dec 22, 2017
1 parent 9a521cb commit 20bbb17
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/ngMessages/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 20bbb17

Please sign in to comment.