diff options
| author | Brian Ford | 2013-10-30 14:51:02 -0700 |
|---|---|---|
| committer | Brian Ford | 2013-10-30 16:21:02 -0700 |
| commit | e19067c9bbac3c3bb450c80f73eb5518bd0db1a1 (patch) | |
| tree | e4e0220a67c2d0c57077ecdd4eedfd85e81c2b3a /src/ng/directive/ngIf.js | |
| parent | 9d0a69772c39bfc751ca2000c3b4b3381e51fe93 (diff) | |
| download | angular.js-e19067c9bbac3c3bb450c80f73eb5518bd0db1a1.tar.bz2 | |
fix(ngIf): ngIf removes elements dynamically added to it
When using ngIf with ngInclude on the same element, ngIf previously did not remove
elements added by ngInclude. Similarly, when using ngIfStart/End, ngIf will miss
elements added between the start/end markers added after ngIf is linked.
This commit changes the behavior of ngIf to add a comment node at the end of its
elements such that elements between the starting comment and this ending comment
are removed when ngIf's predicate does not hold.
Diffstat (limited to 'src/ng/directive/ngIf.js')
| -rw-r--r-- | src/ng/directive/ngIf.js | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/ng/directive/ngIf.js b/src/ng/directive/ngIf.js index b2811ab4..662f3f32 100644 --- a/src/ng/directive/ngIf.js +++ b/src/ng/directive/ngIf.js @@ -86,20 +86,21 @@ var ngIfDirective = ['$animate', function($animate) { restrict: 'A', compile: function (element, attr, transclude) { return function ($scope, $element, $attr) { - var childElement, childScope; + var block = {}, childScope; $scope.$watch($attr.ngIf, function ngIfWatchAction(value) { - if (childElement) { - $animate.leave(childElement); - childElement = undefined; + if (block.startNode) { + $animate.leave(getBlockElements(block)); + block = {}; } - if (childScope) { - childScope.$destroy(); - childScope = undefined; + if (block.startNode) { + getBlockElements(block).$destroy(); + block = {}; } if (toBoolean(value)) { childScope = $scope.$new(); transclude(childScope, function (clone) { - childElement = clone; + block.startNode = clone[0]; + block.endNode = clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' '); $animate.enter(clone, $element.parent(), $element); }); } @@ -107,4 +108,22 @@ var ngIfDirective = ['$animate', function($animate) { }; } }; + + // TODO(bford): this helper was copypasta'd from ngRepeat + function getBlockElements(block) { + if (block.startNode === block.endNode) { + return jqLite(block.startNode); + } + + var element = block.startNode; + var elements = [element]; + + do { + element = element.nextSibling; + if (!element) break; + elements.push(element); + } while (element !== block.endNode); + + return jqLite(elements); + } }]; |
