There is a working plunker
We can create two states. One will NOT use nested views, the second will have them.
// state without nested
.state('pageWithout', {
url: '/{pageId:(?:page1|page3|page5)}',
templateUrl: "page1.tpl.html",
})
// state with nested
.state('pageWith', {
url: '/:pageId',
views : {
'' : {
templateUrl: "page2.tpl.html",
},
'@pageWith' : {
template: "<h2>the SECOND content for state with nested views</h2",
}
}
});
So, the trick is to use some pattern to say which pageId values should go to first state. Check the
which is responsible for the url : 'url def...'
processing
And this way we will have similar links translated into different states, with different nested view structure:
// without nested views
<a href="#/page1">
<a href="#/page3">
<a href="#/page5">
// with nested views
<a href="#/page2">
<a href="#/pageA">
<a href="#/pageB">
Check it here
EXEND, with a child state and some defaults. The udpated plunker
So, we can create few children of our second state:
// children
.state('pageWith.defaultChild', {
url: '/default',
views : {
'' : {
template: "<h3>the pageWith.defaultChild </h3",
}
}
})
.state('pageWith.childWithParams', {
url: '/{id:int}',
views : {
'' : {
template: "<h3>the child with ID param {{$stateParams.id}} </h3",
}
}
});
Based on this solution:
We can decorate our parent state with redirectTo
.state('pageWith', {
url: '/:pageId',
redirectTo: 'pageWith.defaultChild',
...
And just add this simple listener
.run(['$rootScope', '$state', function($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function(evt, to, params) {
if (to.redirectTo) {
evt.preventDefault();
$state.go(to.redirectTo, params)
}
});
}]);
Check the default state redirect here