Skip to content

Commit a0e1d93

Browse files
mheverymatsko
authored andcommitted
fix(ngView): accidentally compiling leaving content
closes: angular#2304
1 parent d96e19f commit a0e1d93

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/ng/directive/ngView.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
194194

195195
if (template) {
196196
clearContent();
197-
animate.enter(jqLite('<div></div>').html(template).contents(), element);
197+
var enterElements = jqLite('<div></div>').html(template).contents();
198+
animate.enter(enterElements, element);
198199

199-
var link = $compile(element.contents()),
200+
var link = $compile(enterElements),
200201
current = $route.current,
201202
controller;
202203

test/ng/directive/ngViewSpec.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
describe('ngView', function() {
44
var element;
55

6-
beforeEach(module(function() {
6+
beforeEach(module(function($provide) {
7+
$provide.value('$window', angular.mock.createMockWindow());
78
return function($rootScope, $compile, $animator) {
89
element = $compile('<ng:view onload="load()"></ng:view>')($rootScope);
910
$animator.enabled(true);
@@ -621,5 +622,46 @@ describe('ngView', function() {
621622
}
622623
}));
623624

625+
626+
it('should not double compile when route changes', function() {
627+
module(function($routeProvider, $animationProvider, $provide) {
628+
$routeProvider.when('/foo', {template: '<div ng-repeat="i in [1,2]">{{i}}</div>'});
629+
$routeProvider.when('/bar', {template: '<div ng-repeat="i in [3,4]">{{i}}</div>'});
630+
$animationProvider.register('my-animation-leave', function() {
631+
return {
632+
start: function(element, done) {
633+
done();
634+
}
635+
};
636+
});
637+
});
638+
639+
inject(function($rootScope, $compile, $location, $route, $window, $rootElement, $sniffer) {
640+
element = $compile(html('<ng:view onload="load()" ng-animate="\'my-animation\'"></ng:view>'))($rootScope);
641+
642+
$location.path('/foo');
643+
$rootScope.$digest();
644+
if ($sniffer.supportsTransitions) {
645+
$window.setTimeout.expect(1).process();
646+
$window.setTimeout.expect(0).process();
647+
}
648+
expect(element.text()).toEqual('12');
649+
650+
$location.path('/bar');
651+
$rootScope.$digest();
652+
expect(n(element.text())).toEqual('1234');
653+
if ($sniffer.supportsTransitions) {
654+
$window.setTimeout.expect(1).process();
655+
$window.setTimeout.expect(1).process();
656+
} else {
657+
$window.setTimeout.expect(1).process();
658+
}
659+
expect(element.text()).toEqual('34');
660+
661+
function n(text) {
662+
return text.replace(/\r\n/m, '').replace(/\r\n/m, '');
663+
}
664+
});
665+
});
624666
});
625-
});
667+
});

0 commit comments

Comments
 (0)