diff --git a/lib/routing/ng_view.dart b/lib/routing/ng_view.dart index a9c489d15..853880f3c 100644 --- a/lib/routing/ng_view.dart +++ b/lib/routing/ng_view.dart @@ -90,6 +90,7 @@ class NgView implements DetachAware, RouteProvider { void detach() { _route.discard(); _locationService._unregisterPortal(this); + _cleanUp(); } void _show(_View viewDef, Route route, List modules) { @@ -128,7 +129,6 @@ class NgView implements DetachAware, RouteProvider { _view.nodes.forEach((node) => node.remove()); _childScope.destroy(); - _view = null; _childScope = null; } diff --git a/test/routing/ng_view_spec.dart b/test/routing/ng_view_spec.dart index f026b2c41..1709ff4f8 100644 --- a/test/routing/ng_view_spec.dart +++ b/test/routing/ng_view_spec.dart @@ -5,7 +5,7 @@ import '../_specs.dart'; import 'package:angular/routing/module.dart'; import 'package:angular/mock/module.dart'; -main() { +main() => describe('ngView', () { describe('Flat ngView', () { TestBed _; Router router; @@ -98,42 +98,65 @@ main() { beforeEach((TestBed tb, Router _router, TemplateCache templates) { _ = tb; router = _router; + _.rootScope.context['flag'] = true; templates.put('library.html', new HttpResponse(200, '

Library

' - '
')); + '')); templates.put('book_list.html', new HttpResponse(200, '

Books

')); templates.put('book_overview.html', new HttpResponse(200, '

Book 1234

')); templates.put('book_read.html', new HttpResponse(200, '

Read Book 1234

')); + templates.put('alt.html', new HttpResponse(200, 'alt')); }); it('should switch nested templates', async(() { Element root = _.compile(''); + microLeap(); _.rootScope.apply(); microLeap(); expect(root.text).toEqual(''); router.route('/library/all'); - microLeap(); + microLeap(); _.rootScope.apply(); microLeap(); expect(root.text).toEqual('LibraryBooks'); router.route('/library/1234'); - microLeap(); + microLeap(); _.rootScope.apply(); microLeap(); expect(root.text).toEqual('LibraryBook 1234'); // nothing should change here router.route('/library/1234/overview'); - microLeap(); + microLeap(); _.rootScope.apply(); microLeap(); expect(root.text).toEqual('LibraryBook 1234'); // nothing should change here router.route('/library/1234/read'); - microLeap(); + microLeap(); _.rootScope.apply(); microLeap(); expect(root.text).toEqual('LibraryRead Book 1234'); })); - }); + it('should not attempt to destroy and already destroyed childscope', async(() { + // This can happen with nested ng-views. Refer + // https://github.com/angular/angular.dart/issues/1182 + // and repro case + // https://github.com/chirayuk/sample/tree/issue_1182_leaving_a_nested_ng_view + Element root = _.compile(''); + microLeap(); _.rootScope.apply(); microLeap(); + + router.route('/library/1234'); + microLeap(); _.rootScope.apply(); microLeap(); + + expect(root.text).toEqual('LibraryBook 1234'); + + _.rootScope.context['flag'] = false; + microLeap(); _.rootScope.apply(); microLeap(); + router.route('/alt'); + microLeap(); _.rootScope.apply(); microLeap(); + + expect(root.text).toEqual('alt'); + })); + }); describe('Inline template ngView', () { TestBed _; @@ -165,7 +188,7 @@ main() { expect(root.text).toEqual('Hello'); })); }); -} +}); class FlatRouteInitializer implements Function { void call(Router router, RouteViewFactory views) { @@ -193,7 +216,10 @@ class NestedRouteInitializer implements Function { 'read': ngRoute(path: '/read', view: 'book_read.html'), 'admin': ngRoute(path: '/admin', view: 'admin.html'), }) - }) + }), + 'alt': ngRoute( + path: '/alt', + view: 'alt.html'), }); } }