Skip to content

Commit 94fa6db

Browse files
Teemu Kokkonenchristopherthielen
Teemu Kokkonen
authored andcommitted
cherry-pick 1be1379 from 0.2.17
1 parent 0fabe35 commit 94fa6db

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

src/ng1/viewDirective.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate,
179179
trace.traceUiViewEvent("Linking", viewData);
180180

181181
function configUpdatedCallback(config?: ViewConfig) {
182-
if (configsEqual(viewConfig, config)) return;
182+
if (configsEqual(viewConfig, config) || scope._willBeDestroyed) return;
183183
trace.traceUiViewConfigUpdated(viewData, config && config.context);
184184

185185
viewConfig = config;
@@ -197,27 +197,42 @@ function $ViewDirective( $view, $animate, $uiViewScroll, $interpolate,
197197
});
198198

199199
function cleanupLastView() {
200-
if (previousEl) {
201-
trace.traceUiViewEvent("Removing (previous) el", viewData);
202-
previousEl.remove();
203-
previousEl = null;
200+
var _previousEl = previousEl;
201+
var _currentScope = currentScope;
202+
203+
if (_currentScope) {
204+
_currentScope._willBeDestroyed = true;
204205
}
205206

206-
if (currentScope) {
207-
trace.traceUiViewEvent("Destroying (previous) scope", viewData);
208-
currentScope.$destroy();
209-
currentScope = null;
207+
function cleanOld() {
208+
if (_previousEl) {
209+
trace.traceUiViewEvent("Removing (previous) el", viewData);
210+
_previousEl.remove();
211+
_previousEl = null;
212+
}
213+
214+
if (_currentScope) {
215+
trace.traceUiViewEvent("Destroying (previous) scope", viewData);
216+
_currentScope.$destroy();
217+
_currentScope = null;
218+
}
210219
}
211220

212221
if (currentEl) {
213222
trace.traceUiViewEvent("Animate out (previous)", viewData);
214223
renderer.leave(currentEl, function() {
224+
cleanOld();
215225
previousEl = null;
216226
});
217227

218228
previousEl = currentEl;
219-
currentEl = null;
229+
} else {
230+
cleanOld();
231+
previousEl = null;
220232
}
233+
234+
currentEl = null;
235+
currentScope = null;
221236
}
222237

223238
function updateView(config?: ViewConfig) {

test/viewDirectiveSpec.js

+44-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var uiRouter = require("angular-ui-router");
44
describe('uiView', function () {
55
'use strict';
66

7-
var scope, $compile, elem;
7+
var log, scope, $compile, elem;
88

99
beforeEach(function() {
1010
var depends = ['ui.router'];
@@ -27,6 +27,10 @@ describe('uiView', function () {
2727
});
2828
}));
2929

30+
beforeEach(function() {
31+
log = '';
32+
});
33+
3034
var aState = {
3135
template: 'aState template'
3236
},
@@ -98,8 +102,9 @@ describe('uiView', function () {
98102
}
99103
}
100104
},
101-
mState = {
102-
template: 'mState',
105+
106+
oState = {
107+
template: 'oState',
103108
controller: function ($scope, $element) {
104109
$scope.elementId = $element.attr('id');
105110
}
@@ -119,7 +124,22 @@ describe('uiView', function () {
119124
.state('j', jState)
120125
.state('k', kState)
121126
.state('l', lState)
122-
.state('m', mState)
127+
.state('m', {
128+
template: 'mState',
129+
controller: function($scope) {
130+
log += 'm;';
131+
$scope.$on('$destroy', function() {
132+
log += '$destroy(m);';
133+
});
134+
},
135+
})
136+
.state('n', {
137+
template: 'nState',
138+
controller: function($scope) {
139+
log += 'n;';
140+
},
141+
})
142+
.state('o', oState)
123143
}));
124144

125145
beforeEach(inject(function ($rootScope, _$compile_) {
@@ -130,6 +150,23 @@ describe('uiView', function () {
130150

131151
describe('linking ui-directive', function () {
132152

153+
it('$destroy event is triggered after animation ends', inject(function($state, $q, $animate) {
154+
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
155+
156+
$state.transitionTo('m');
157+
$q.flush();
158+
expect(log).toBe('m;');
159+
$state.transitionTo('n');
160+
$q.flush();
161+
if ($animate) {
162+
expect(log).toBe('m;n;');
163+
$animate.triggerCallbacks();
164+
expect(log).toBe('m;n;$destroy(m);');
165+
} else {
166+
expect(log).toBe('m;$destroy(m);n;');
167+
}
168+
}));
169+
133170
it('anonymous ui-view should be replaced with the template of the current $state', inject(function ($state, $q) {
134171
elem.append($compile('<div><ui-view></ui-view></div>')(scope));
135172

@@ -315,11 +352,11 @@ describe('uiView', function () {
315352
}));
316353

317354
it('should instantiate a controller with both $scope and $element injections', inject(function ($state, $q) {
318-
elem.append($compile('<div><ui-view id="mState">{{elementId}}</ui-view></div>')(scope));
319-
$state.transitionTo(mState);
355+
elem.append($compile('<div><ui-view id="oState">{{elementId}}</ui-view></div>')(scope));
356+
$state.transitionTo(oState);
320357
$q.flush();
321358

322-
expect(elem.text()).toBe('mState');
359+
expect(elem.text()).toBe('oState');
323360
}));
324361

325362
describe('play nicely with other directives', function() {

0 commit comments

Comments
 (0)