From 3949438c703f8c3e0a8c3b3e9cc777171f791620 Mon Sep 17 00:00:00 2001 From: Jakub Strojewski Date: Tue, 12 Nov 2013 15:21:07 +0100 Subject: [PATCH] =?UTF-8?q?fix(ngIf):=20don't=20create=20multiple=20elemen?= =?UTF-8?q?ts=20when=20changing=20from=20a=20truthy=E2=80=A6=20=E2=80=A6?= =?UTF-8?q?=20to=20another=20thruthy=20value.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (another way of fixing it) Fixes #4852. --- src/ng/directive/ngIf.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/ng/directive/ngIf.js b/src/ng/directive/ngIf.js index 6e845e289952..697e2f925708 100644 --- a/src/ng/directive/ngIf.js +++ b/src/ng/directive/ngIf.js @@ -79,7 +79,7 @@ */ -var ngIfDirective = ['$animate', function($animate) { +var ngIfDirective = ['$animate', '$parse', function($animate, $parse) { return { transclude: 'element', priority: 600, @@ -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)); + }, + function ngIfWatchAction(value) { + if (value) { childScope = $scope.$new(); transclude(childScope, function (clone) { block = { @@ -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; + } } } - }); + ); }; } };