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

Commit 4612705

Browse files
committed
fix(ngIf): don't create multiple elements when changing from a truthy to another thruthy value.
Fixes #4852.
1 parent 9577702 commit 4612705

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/ng/directive/ngIf.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
}
6161
6262
/*
63-
The transition styles can also be placed on the CSS base class above
63+
The transition styles can also be placed on the CSS base class above
6464
*/
6565
.animate-if.ng-enter, .animate-if.ng-leave {
6666
-webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
@@ -92,16 +92,16 @@ var ngIfDirective = ['$animate', function($animate) {
9292
$scope.$watch($attr.ngIf, function ngIfWatchAction(value) {
9393

9494
if (toBoolean(value)) {
95-
96-
childScope = $scope.$new();
97-
transclude(childScope, function (clone) {
98-
block = {
99-
startNode: clone[0],
100-
endNode: clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ')
101-
};
102-
$animate.enter(clone, $element.parent(), $element);
103-
});
104-
95+
if (!childScope) {
96+
childScope = $scope.$new();
97+
transclude(childScope, function (clone) {
98+
block = {
99+
startNode: clone[0],
100+
endNode: clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ')
101+
};
102+
$animate.enter(clone, $element.parent(), $element);
103+
});
104+
}
105105
} else {
106106

107107
if (childScope) {

test/ng/directive/ngIfSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ describe('ngIf', function () {
2828
expect(element.children().length).toBe(1);
2929
});
3030

31+
it('should not add the element twice if the condition goes from true to true', function () {
32+
$scope.hello = 'true1';
33+
makeIf('hello');
34+
expect(element.children().length).toBe(1);
35+
$scope.$apply('hello = "true2"');
36+
expect(element.children().length).toBe(1);
37+
});
38+
39+
it('should not recreate the element if the condition goes from true to true', function () {
40+
$scope.hello = 'true1';
41+
makeIf('hello');
42+
element.children().data('flag', true);
43+
$scope.$apply('hello = "true2"');
44+
expect(element.children().data('flag')).toBe(true);
45+
});
46+
3147
it('should create then remove the element if condition changes', function () {
3248
$scope.hello = true;
3349
makeIf('hello');

0 commit comments

Comments
 (0)