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

Commit f6e189d

Browse files
committed
refactor(ngAnimateSwap): simplify scope creation add tests regarding creating/removing scopes
Closes #16565
1 parent c0b01d9 commit f6e189d

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/ngAnimate/ngAnimateSwap.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ var ngAnimateSwapDirective = ['$animate', function($animate) {
104104
previousScope = null;
105105
}
106106
if (value || value === 0) {
107-
previousScope = scope.$new();
108-
$transclude(previousScope, function(element) {
109-
previousElement = element;
110-
$animate.enter(element, null, $element);
107+
$transclude(function(clone, childScope) {
108+
previousElement = clone;
109+
previousScope = childScope;
110+
$animate.enter(clone, null, $element);
111111
});
112112
}
113113
});

test/ngAnimate/ngAnimateSwapSpec.js

+47
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,53 @@ describe('ngAnimateSwap', function() {
132132
expect(two).toBeTruthy();
133133
}));
134134

135+
it('should create a new (non-isolate) scope for each inserted clone', inject(function() {
136+
var parentScope = $rootScope.$new();
137+
parentScope.foo = 'bar';
138+
139+
element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')(parentScope);
140+
141+
$rootScope.$apply('value = 1');
142+
var scopeOne = element.find('div').eq(0).scope();
143+
expect(scopeOne.foo).toBe('bar');
144+
145+
$rootScope.$apply('value = 2');
146+
var scopeTwo = element.find('div').eq(0).scope();
147+
expect(scopeTwo.foo).toBe('bar');
148+
149+
expect(scopeOne).not.toBe(scopeTwo);
150+
}));
151+
152+
it('should destroy the previous scope when removing the element', inject(function() {
153+
element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')($rootScope);
154+
155+
$rootScope.$apply('value = 1');
156+
var scopeOne = element.find('div').eq(0).scope();
157+
expect(scopeOne.$$destroyed).toBe(false);
158+
159+
// Swapping the old element with a new one.
160+
$rootScope.$apply('value = 2');
161+
expect(scopeOne.$$destroyed).toBe(true);
162+
163+
var scopeTwo = element.find('div').eq(0).scope();
164+
expect(scopeTwo.$$destroyed).toBe(false);
165+
166+
// Removing the old element (without inserting a new one).
167+
$rootScope.$apply('value = null');
168+
expect(scopeTwo.$$destroyed).toBe(true);
169+
}));
170+
171+
it('should destroy the previous scope when swapping elements', inject(function() {
172+
element = $compile('<div><div ng-animate-swap="value">{{ value }}</div></div>')($rootScope);
173+
174+
$rootScope.$apply('value = 1');
175+
var scopeOne = element.find('div').eq(0).scope();
176+
expect(scopeOne.$$destroyed).toBe(false);
177+
178+
$rootScope.$apply('value = 2');
179+
expect(scopeOne.$$destroyed).toBe(true);
180+
}));
181+
135182

136183
describe('animations', function() {
137184
it('should trigger a leave animation followed by an enter animation upon swap',

0 commit comments

Comments
 (0)