diff --git a/src/ngAnimate/ngAnimateSwap.js b/src/ngAnimate/ngAnimateSwap.js
index 3b91ecc1ea5a..83c128bb8944 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',
@@ -104,10 +104,10 @@ var ngAnimateSwapDirective = ['$animate', '$rootScope', function($animate, $root
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('
')(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('')($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('')($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',