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

Commit b9001e9

Browse files
vojtajinaIgorMinar
authored andcommitted
fix(route): $destroy scope after update and reload
When we update route (changing only search param, no route reload) and then reload (change to different route), it did not $destroy last scope.
1 parent d1e7a53 commit b9001e9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/service/route.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ function $RouteProvider(){
258258

259259
if (next && last && next.$route === last.$route
260260
&& equals(next.pathParams, last.pathParams) && !next.reloadOnSearch && !forceReload) {
261+
next.scope = last.scope;
261262
$route.current = next;
262263
copy(next.params, $routeParams);
263264
last.scope && last.scope.$emit('$routeUpdate');

test/service/routeSpec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,43 @@ describe('$route', function() {
401401
}));
402402

403403

404+
it('should $destroy scope after update and reload',
405+
inject(function($route, $location, $rootScope) {
406+
// this is a regression of bug, where $route doesn't copy scope when only updating
407+
408+
var log = [];
409+
410+
function logger(msg) {
411+
return function() {
412+
log.push(msg);
413+
};
414+
}
415+
416+
function createController(name) {
417+
return function() {
418+
log.push('init-' + name);
419+
this.$on('$destroy', logger('destroy-' + name));
420+
this.$on('$routeUpdate', logger('route-update'));
421+
};
422+
}
423+
424+
$route.when('/foo', {controller: createController('foo'), reloadOnSearch: false});
425+
$route.when('/bar', {controller: createController('bar')});
426+
427+
$location.url('/foo');
428+
$rootScope.$digest();
429+
expect(log).toEqual(['init-foo']);
430+
431+
$location.search({q: 'some'});
432+
$rootScope.$digest();
433+
expect(log).toEqual(['init-foo', 'route-update']);
434+
435+
$location.url('/bar');
436+
$rootScope.$digest();
437+
expect(log).toEqual(['init-foo', 'route-update', 'destroy-foo', 'init-bar']);
438+
}));
439+
440+
404441
describe('reload', function() {
405442

406443
it('should reload even if reloadOnSearch is false',

0 commit comments

Comments
 (0)