From a24548357db84842fbe832548ed803b6ceff3f9a Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Tue, 15 May 2018 17:16:20 +0300 Subject: [PATCH 1/2] refactor(ngAnimateSwap): remove usused dependency --- src/ngAnimate/ngAnimateSwap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngAnimate/ngAnimateSwap.js b/src/ngAnimate/ngAnimateSwap.js index 3b91ecc1ea5a..32ad77637ef9 100644 --- a/src/ngAnimate/ngAnimateSwap.js +++ b/src/ngAnimate/ngAnimateSwap.js @@ -87,7 +87,7 @@ * * */ -var ngAnimateSwapDirective = ['$animate', '$rootScope', function($animate, $rootScope) { +var ngAnimateSwapDirective = ['$animate', function($animate) { return { restrict: 'A', transclude: 'element', From 431dc5eeffa0061bed2640b674132b4d4d09a64e Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Tue, 15 May 2018 17:17:44 +0300 Subject: [PATCH 2/2] refactor(ngAnimateSwap): simplify scope creation add tests regarding creating/removing scopes --- src/ngAnimate/ngAnimateSwap.js | 8 ++--- test/ngAnimate/ngAnimateSwapSpec.js | 47 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/ngAnimate/ngAnimateSwap.js b/src/ngAnimate/ngAnimateSwap.js index 32ad77637ef9..83c128bb8944 100644 --- a/src/ngAnimate/ngAnimateSwap.js +++ b/src/ngAnimate/ngAnimateSwap.js @@ -104,10 +104,10 @@ var ngAnimateSwapDirective = ['$animate', function($animate) { previousScope = null; } if (value || value === 0) { - previousScope = scope.$new(); - $transclude(previousScope, function(element) { - previousElement = element; - $animate.enter(element, null, $element); + $transclude(function(clone, childScope) { + previousElement = clone; + previousScope = childScope; + $animate.enter(clone, null, $element); }); } }); diff --git a/test/ngAnimate/ngAnimateSwapSpec.js b/test/ngAnimate/ngAnimateSwapSpec.js index e611f72d57db..4cfedce7e0ce 100644 --- a/test/ngAnimate/ngAnimateSwapSpec.js +++ b/test/ngAnimate/ngAnimateSwapSpec.js @@ -132,6 +132,53 @@ describe('ngAnimateSwap', function() { expect(two).toBeTruthy(); })); + it('should create a new (non-isolate) scope for each inserted clone', inject(function() { + var parentScope = $rootScope.$new(); + parentScope.foo = 'bar'; + + element = $compile('
{{ value }}
')(parentScope); + + $rootScope.$apply('value = 1'); + var scopeOne = element.find('div').eq(0).scope(); + expect(scopeOne.foo).toBe('bar'); + + $rootScope.$apply('value = 2'); + var scopeTwo = element.find('div').eq(0).scope(); + expect(scopeTwo.foo).toBe('bar'); + + expect(scopeOne).not.toBe(scopeTwo); + })); + + it('should destroy the previous scope when removing the element', inject(function() { + element = $compile('
{{ value }}
')($rootScope); + + $rootScope.$apply('value = 1'); + var scopeOne = element.find('div').eq(0).scope(); + expect(scopeOne.$$destroyed).toBe(false); + + // Swapping the old element with a new one. + $rootScope.$apply('value = 2'); + expect(scopeOne.$$destroyed).toBe(true); + + var scopeTwo = element.find('div').eq(0).scope(); + expect(scopeTwo.$$destroyed).toBe(false); + + // Removing the old element (without inserting a new one). + $rootScope.$apply('value = null'); + expect(scopeTwo.$$destroyed).toBe(true); + })); + + it('should destroy the previous scope when swapping elements', inject(function() { + element = $compile('
{{ value }}
')($rootScope); + + $rootScope.$apply('value = 1'); + var scopeOne = element.find('div').eq(0).scope(); + expect(scopeOne.$$destroyed).toBe(false); + + $rootScope.$apply('value = 2'); + expect(scopeOne.$$destroyed).toBe(true); + })); + describe('animations', function() { it('should trigger a leave animation followed by an enter animation upon swap',