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

fix(ngIf): don't create multiple elements when changing from a truthy… #4915

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/ng/directive/ngIf.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
</file>
</example>
*/
var ngIfDirective = ['$animate', function($animate) {
var ngIfDirective = ['$animate', '$parse', function($animate, $parse) {
return {
transclude: 'element',
priority: 600,
Expand All @@ -89,10 +89,12 @@ var ngIfDirective = ['$animate', function($animate) {
compile: function (element, attr, transclude) {
return function ($scope, $element, $attr) {
var block, childScope;
$scope.$watch($attr.ngIf, function ngIfWatchAction(value) {

if (toBoolean(value)) {
if (!childScope) {
$scope.$watch(
function ngIfWatchExpression() {
return toBoolean($parse($attr.ngIf)($scope));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$scope.$eval() will pretty much do this for you, but is it really important to re-$parse every $digest? that seems a bit like overkill since the expression is unlikely to change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just another suggestion, first (and better I think) is here:
#4911

},
function ngIfWatchAction(value) {
if (value) {
childScope = $scope.$new();
transclude(childScope, function (clone) {
block = {
Expand All @@ -101,20 +103,19 @@ var ngIfDirective = ['$animate', function($animate) {
};
$animate.enter(clone, $element.parent(), $element);
});
}
} else {

if (childScope) {
childScope.$destroy();
childScope = null;
}

if (block) {
$animate.leave(getBlockElements(block));
block = null;
} else {
if (childScope) {
childScope.$destroy();
childScope = null;
}

if (block) {
$animate.leave(getBlockElements(block));
block = null;
}
}
}
});
);
};
}
};
Expand Down