有a working plunker
我们可以创建两个状态。一个不会使用嵌套视图,第二个会有它们。
// 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",
}
}
});
因此,诀窍是使用某种模式来说明哪些 pageId 值应该进入第一个状态。检查
负责url : 'url def...'处理
这样我们就可以将相似的链接翻译成不同的状态,具有不同的嵌套视图结构:
// without nested views
<a href="#/page1">
<a href="#/page3">
<a href="#/page5">
// with nested views
<a href="#/page2">
<a href="#/pageA">
<a href="#/pageB">
查看it here
EXEND,带有子状态和一些默认值。 udpated plunker
所以,我们可以创建第二个状态的几个子节点:
// 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",
}
}
});
基于此解决方案:
我们可以用 redirectTo
来装饰我们的父状态
.state('pageWith', {
url: '/:pageId',
redirectTo: 'pageWith.defaultChild',
...
只需添加这个简单的监听器
.run(['$rootScope', '$state', function($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function(evt, to, params) {
if (to.redirectTo) {
evt.preventDefault();
$state.go(to.redirectTo, params)
}
});
}]);
勾选默认state redirect here