Skip to content

Commit 8fe5b1f

Browse files
fix(view): Allow targeting nested named ui-view by simple ui-view name
Closes #3355
1 parent ec6e5e4 commit 8fe5b1f

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/directives/viewDirective.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ function $ViewDirective($view: ViewService, $animate: any, $uiViewScroll: any, $
214214
config: null, // The ViewConfig loaded (from a state.views definition)
215215
configUpdated: configUpdatedCallback, // Called when the matching ViewConfig changes
216216
get creationContext() { // The context in which this ui-view "tag" was created
217-
return parse('$cfg.viewDecl.$context')(inherited);
217+
let fromParentTagConfig = parse('$cfg.viewDecl.$context')(inherited);
218+
// Allow <ui-view name="foo"><ui-view name="bar"></ui-view></ui-view>
219+
// See https://github.com/angular-ui/ui-router/issues/3355
220+
let fromParentTag = parse('$uiView.creationContext')(inherited);
221+
return fromParentTagConfig || fromParentTag;
218222
}
219223
};
220224

test/viewDirectiveSpec.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -722,17 +722,39 @@ describe("UiView", function() {
722722
beforeEach(module(function($stateProvider) {
723723
$stateProvider
724724
.state('main', { abstract: true, views: { main: {} } })
725-
.state('main.home', { views: { content: { template: 'home.html' } } });
725+
.state('main.home', { views: { content: { template: 'HOME' } } })
726+
.state('test', { views: { 'nest': { template: 'TEST' } } });
726727
}));
727728

728-
it("shouldn't puke on weird view setups", inject(function($compile, $rootScope, $q, $state) {
729+
it("shouldn't puke on weird nested view setups", inject(function($compile, $rootScope, $q, $state) {
729730
$compile('<div ui-view="main"><div ui-view="content"></div></div>')($rootScope);
730731

731732
$state.go('main.home');
732733
$q.flush();
733734

734735
expect($state.current.name).toBe('main.home');
735736
}));
737+
738+
// Test for https://github.com/angular-ui/ui-router/issues/3355
739+
it("should target weird nested view setups using the view's simple name", inject(function($compile, $rootScope, $q, $state) {
740+
let tpl = `
741+
<div>
742+
<div ui-view="main">
743+
MAIN-DEFAULT-
744+
<div ui-view="content">
745+
<div ui-view="nest"></div>
746+
</div>
747+
</div>
748+
</div>
749+
`;
750+
let el = $compile(tpl)($rootScope);
751+
752+
$state.go('test');
753+
$q.flush();
754+
755+
expect($state.current.name).toBe('test');
756+
expect(el.text().replace(/\s*/g, "")).toBe('MAIN-DEFAULT-TEST');
757+
}));
736758
});
737759

738760
describe('uiView transclusion', function() {

0 commit comments

Comments
 (0)