Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f9104d1

Browse files
committed
fix memory leak, style updates, add test for order with async templats
1 parent 8b68680 commit f9104d1

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/ngMessages/messages.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ angular.module('ngMessages', [])
410410

411411
$scope.$watchCollection($attrs.ngMessages || $attrs['for'], ctrl.render);
412412

413+
// If the element is destroyed, proactively destroy all the currently visible messages
414+
$element.on('$destroy', function() {
415+
forEach(messages, function(item) {
416+
item.message.detach();
417+
});
418+
})
419+
413420
this.reRender = function() {
414421
if (!renderLater) {
415422
renderLater = true;
@@ -670,8 +677,8 @@ function ngMessageDirectiveFactory() {
670677
// when we are destroying the node later.
671678
var $$attachId = currentElement.$$attachId = ngMessagesCtrl.getAttachId();
672679

673-
// in the event that the parent element is destroyed
674-
// by any other structural directive then it's time
680+
// in the event that the element or a parent element is destroyed
681+
// by another structural directive then it's time
675682
// to deregister the message from the controller
676683
currentElement.on('$destroy', function() {
677684
if (currentElement && currentElement.$$attachId === $$attachId) {

test/ngMessages/messagesSpec.js

+41-9
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ describe('ngMessages', function() {
412412
}));
413413

414414

415-
it('should not crash when the messages are transcluded, the first message is visible, and ngMessages is removed by ngIf', function() {
415+
it('should not crash or leak memory when the messages are transcluded, the first message is' +
416+
'visible, and ngMessages is removed by ngIf', function() {
416417

417418
module(function($compileProvider) {
418419
$compileProvider.directive('messageWrap', function() {
@@ -444,14 +445,6 @@ describe('ngMessages', function() {
444445
expect(messageChildren(element).length).toBe(1);
445446
expect(trim(element.text())).toEqual('Fill in the text field.');
446447

447-
$rootScope.$apply(function() {
448-
$rootScope.col.required = true;
449-
$rootScope.col.extra = false;
450-
});
451-
452-
expect(messageChildren(element).length).toBe(1);
453-
expect(trim(element.text())).toEqual('Fill in the text field.');
454-
455448
$rootScope.$apply('show = false');
456449

457450
expect(messageChildren(element).length).toBe(0);
@@ -864,4 +857,43 @@ describe('ngMessages', function() {
864857
expect(s(element.text())).toEqual("YYYZZZX");
865858
}));
866859
});
860+
861+
it('should sustain the order when messages are included by a template with an async template', function() {
862+
module(function($compileProvider) {
863+
$compileProvider.directive('inclusor', function() {
864+
return {
865+
templateUrl: 'include.html'
866+
};
867+
});
868+
});
869+
870+
inject(function($compile, $rootScope, $templateCache) {
871+
var html;
872+
$templateCache.put('include.html', '<div ng-message="a">A</div>' +
873+
'<div ng-message="b">B</div>');
874+
875+
html = '<div ng-messages-multiple ng-messages="items"><div inclusor>' +
876+
'</div><div ng-message="c">C</div><div ng-message="d">D</div></div>';
877+
$rootScope.items = {};
878+
879+
$rootScope.$apply(function() {
880+
$rootScope.items.b = true;
881+
$rootScope.items.d = true;
882+
});
883+
884+
element = $compile(html)($rootScope);
885+
$rootScope.$digest();
886+
887+
expect(element.text().trim()).toBe('BD');
888+
889+
$rootScope.$apply(function() {
890+
$rootScope.items.a = true;
891+
$rootScope.items.c = true;
892+
});
893+
894+
expect(element.text().trim()).toBe('ABCD');
895+
});
896+
897+
});
898+
867899
});

0 commit comments

Comments
 (0)