Skip to content

Commit 4648d7e

Browse files
author
Teemu Kokkonen
committed
fix(viewDirective): $destroy event is triggered before animation ends
when view is animated using ngAnimate, $destroy event is triggered before animation ends. closes angular-ui#1643
1 parent d23e9fa commit 4648d7e

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

src/viewDirective.js

+39-26
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@
2626
* functionality, call `$uiViewScrollProvider.useAnchorScroll()`.*
2727
*
2828
* @param {string=} onload Expression to evaluate whenever the view updates.
29-
*
29+
*
3030
* @example
31-
* A view can be unnamed or named.
31+
* A view can be unnamed or named.
3232
* <pre>
3333
* <!-- Unnamed -->
34-
* <div ui-view></div>
35-
*
34+
* <div ui-view></div>
35+
*
3636
* <!-- Named -->
3737
* <div ui-view="viewName"></div>
3838
* </pre>
3939
*
40-
* You can only have one unnamed view within any template (or root html). If you are only using a
40+
* You can only have one unnamed view within any template (or root html). If you are only using a
4141
* single view and it is unnamed then you can populate it like so:
4242
* <pre>
43-
* <div ui-view></div>
43+
* <div ui-view></div>
4444
* $stateProvider.state("home", {
4545
* template: "<h1>HELLO!</h1>"
4646
* })
4747
* </pre>
48-
*
48+
*
4949
* The above is a convenient shortcut equivalent to specifying your view explicitly with the {@link ui.router.state.$stateProvider#views `views`}
5050
* config property, by name, in this case an empty name:
5151
* <pre>
@@ -54,33 +54,33 @@
5454
* "": {
5555
* template: "<h1>HELLO!</h1>"
5656
* }
57-
* }
57+
* }
5858
* })
5959
* </pre>
60-
*
61-
* But typically you'll only use the views property if you name your view or have more than one view
62-
* in the same template. There's not really a compelling reason to name a view if its the only one,
60+
*
61+
* But typically you'll only use the views property if you name your view or have more than one view
62+
* in the same template. There's not really a compelling reason to name a view if its the only one,
6363
* but you could if you wanted, like so:
6464
* <pre>
6565
* <div ui-view="main"></div>
66-
* </pre>
66+
* </pre>
6767
* <pre>
6868
* $stateProvider.state("home", {
6969
* views: {
7070
* "main": {
7171
* template: "<h1>HELLO!</h1>"
7272
* }
73-
* }
73+
* }
7474
* })
7575
* </pre>
76-
*
76+
*
7777
* Really though, you'll use views to set up multiple views:
7878
* <pre>
7979
* <div ui-view></div>
80-
* <div ui-view="chart"></div>
81-
* <div ui-view="data"></div>
80+
* <div ui-view="chart"></div>
81+
* <div ui-view="data"></div>
8282
* </pre>
83-
*
83+
*
8484
* <pre>
8585
* $stateProvider.state("home", {
8686
* views: {
@@ -93,7 +93,7 @@
9393
* "data": {
9494
* template: "<data_thing/>"
9595
* }
96-
* }
96+
* }
9797
* })
9898
* </pre>
9999
*
@@ -190,32 +190,45 @@ function $ViewDirective( $state, $injector, $uiViewScroll, $interpolate)
190190
updateView(true);
191191

192192
function cleanupLastView() {
193-
if (previousEl) {
194-
previousEl.remove();
195-
previousEl = null;
193+
var _previousEl = previousEl;
194+
var _currentScope = currentScope;
195+
196+
if (_currentScope) {
197+
_currentScope._willBeDestroyed = true;
196198
}
197199

198-
if (currentScope) {
199-
currentScope.$destroy();
200-
currentScope = null;
200+
function cleanOld() {
201+
if (_previousEl) {
202+
_previousEl.remove();
203+
}
204+
205+
if (_currentScope) {
206+
_currentScope.$destroy();
207+
}
201208
}
202209

203210
if (currentEl) {
204211
renderer.leave(currentEl, function() {
212+
cleanOld();
205213
previousEl = null;
206214
});
207215

208216
previousEl = currentEl;
209-
currentEl = null;
217+
} else {
218+
cleanOld();
219+
previousEl = null;
210220
}
221+
222+
currentEl = null;
223+
currentScope = null;
211224
}
212225

213226
function updateView(firstTime) {
214227
var newScope,
215228
name = getUiViewName(scope, attrs, $element, $interpolate),
216229
previousLocals = name && $state.$current && $state.$current.locals[name];
217230

218-
if (!firstTime && previousLocals === latestLocals) return; // nothing to do
231+
if (!firstTime && previousLocals === latestLocals || scope._willBeDestroyed) return; // nothing to do
219232
newScope = scope.$new();
220233
latestLocals = $state.$current.locals[name];
221234

0 commit comments

Comments
 (0)