diff --git a/src/ngMessages/messages.js b/src/ngMessages/messages.js index 8805eea81945..6ed575da879c 100644 --- a/src/ngMessages/messages.js +++ b/src/ngMessages/messages.js @@ -684,7 +684,7 @@ function ngMessageDirectiveFactory() { }, attach: function() { if (!currentElement) { - $transclude(scope, function(elm) { + $transclude(function(elm, newScope) { $animate.enter(elm, null, element); currentElement = elm; @@ -700,6 +700,7 @@ function ngMessageDirectiveFactory() { ngMessagesCtrl.deregister(commentNode); messageCtrl.detach(); } + newScope.$destroy(); }); }); } diff --git a/test/ngMessages/messagesSpec.js b/test/ngMessages/messagesSpec.js index 79457fa0276b..5bfbb0898c0d 100644 --- a/test/ngMessages/messagesSpec.js +++ b/test/ngMessages/messagesSpec.js @@ -609,6 +609,34 @@ describe('ngMessages', function() { }); }); + + it('should clean-up the ngMessage scope when a message is removed', + inject(function($compile, $rootScope) { + + var html = + '
' + + '
{{forA}}
' + + '
'; + + element = $compile(html)($rootScope); + $rootScope.$apply(function() { + $rootScope.forA = 'A'; + $rootScope.items = {a: true}; + }); + + expect(element.text()).toBe('A'); + var watchers = $rootScope.$countWatchers(); + + $rootScope.$apply('items.a = false'); + + expect(element.text()).toBe(''); + // We don't know exactly how many watchers are on the scope, only that there should be + // one less now + expect($rootScope.$countWatchers()).toBe(watchers - 1); + }) + ); + + describe('when including templates', function() { they('should work with a dynamic collection model which is managed by ngRepeat', {'
': '
' +