Angular ui-路由器:容器视图和默认视图
Angular ui-router : Container view and Default view
我正在努力为下一个状态创建一个容器,将状态定义为视图,分为页眉、容器、页脚。
下一个状态是博客,但我没有看到将其纳入视图的方法。
一个想法是将 HOME 视图作为标准启动,但也失败了。
查看:
<main>
<header ui-view="header"></header>
<content ui-view="home"></content>
<footer ui-view="footer"></footer>
</main>
状态:
.state('home',{
url:'/',
data: {
pageTitle: 'Home'
},
views: {
'': {
templateUrl: 'content/index.html',
},
'header@home': {
templateUrl: 'content/templates/header.html',
controller: 'HeaderController',
cache: false
},
'home@home': {
templateUrl: 'content/templates/home.html',
controller: 'IndexController',
cache: false
},
'footer@home': {
templateUrl: 'content/templates/footer.html',
//controller: 'FooterController',
cache: false
}
}
})
.state('home.blog',{
url : '/blog',
templateUrl : 'content/templates/blog.html',
controller : 'BlogController',
data: { pageTitle: 'Blog' },
access: {requiredLogin: false}
})
成功! :)
Plunker 示例:http://plnkr.co/edit/yRgqiAeEVQl2WVajGgG0?p=preview
在上面更新的问题中,您使用了this plunker to show您是如何让它工作的:
.state('home',{
url:'',
abstract: true,
views: {
'': {
templateUrl: 'index.html' // this
},
'header@home': {
templateUrl: 'header.html'
},
'footer@home': {
templateUrl: 'footer.html'
}
}
})
.state('home.index',{
url : '/',
templateUrl : 'home.html'
})
.state('home.blog',{
url : '/blog',
templateUrl : 'blog.html',
});
虽然该解决方案有效,但实际上不是 you/we 应该采用的方式。因为父状态'home',注入未命名视图itslef
- templateUrl: 'index.html'
所以,现在又出现了视图 header 和 footer,但它们与根 (原始index.htm
)。他们的绝对名称是 'header@home' 和 'footer@home' (在代码片段中使用) - 一切似乎都在工作。
但这是多余的。除非我们将布局移动到某些 'layout' 状态和 'layout.html'
- Angular UI Router - Nested States with multiple layouts
为什么多余?因为 index.html
已经在播放 (作为 root
) 并且它包含这些目标。它们的绝对名称是 'header@' 和 'footer@'。这应该是要走的路。
为了清楚起见,有an updated plunker及其片段:
.state('home',{
url:'',
abstract: true,
views: {
'': {
template: '<div ui-view=""></div>'
},
'header': {
templateUrl: 'header.html'
},
'footer': {
templateUrl: 'footer.html'
}
}
})
.state('home.index',{
url : '/',
templateUrl : 'home.html'
})
.state('home.blog',{
url : '/blog',
templateUrl : 'blog.html',
});
检查更新here
我正在努力为下一个状态创建一个容器,将状态定义为视图,分为页眉、容器、页脚。
下一个状态是博客,但我没有看到将其纳入视图的方法。
一个想法是将 HOME 视图作为标准启动,但也失败了。
查看:
<main>
<header ui-view="header"></header>
<content ui-view="home"></content>
<footer ui-view="footer"></footer>
</main>
状态:
.state('home',{
url:'/',
data: {
pageTitle: 'Home'
},
views: {
'': {
templateUrl: 'content/index.html',
},
'header@home': {
templateUrl: 'content/templates/header.html',
controller: 'HeaderController',
cache: false
},
'home@home': {
templateUrl: 'content/templates/home.html',
controller: 'IndexController',
cache: false
},
'footer@home': {
templateUrl: 'content/templates/footer.html',
//controller: 'FooterController',
cache: false
}
}
})
.state('home.blog',{
url : '/blog',
templateUrl : 'content/templates/blog.html',
controller : 'BlogController',
data: { pageTitle: 'Blog' },
access: {requiredLogin: false}
})
成功! :)
Plunker 示例:http://plnkr.co/edit/yRgqiAeEVQl2WVajGgG0?p=preview
在上面更新的问题中,您使用了this plunker to show您是如何让它工作的:
.state('home',{
url:'',
abstract: true,
views: {
'': {
templateUrl: 'index.html' // this
},
'header@home': {
templateUrl: 'header.html'
},
'footer@home': {
templateUrl: 'footer.html'
}
}
})
.state('home.index',{
url : '/',
templateUrl : 'home.html'
})
.state('home.blog',{
url : '/blog',
templateUrl : 'blog.html',
});
虽然该解决方案有效,但实际上不是 you/we 应该采用的方式。因为父状态'home',注入未命名视图itslef
- templateUrl: 'index.html'
所以,现在又出现了视图 header 和 footer,但它们与根 (原始index.htm
)。他们的绝对名称是 'header@home' 和 'footer@home' (在代码片段中使用) - 一切似乎都在工作。
但这是多余的。除非我们将布局移动到某些 'layout' 状态和 'layout.html'
- Angular UI Router - Nested States with multiple layouts
为什么多余?因为 index.html
已经在播放 (作为 root
) 并且它包含这些目标。它们的绝对名称是 'header@' 和 'footer@'。这应该是要走的路。
为了清楚起见,有an updated plunker及其片段:
.state('home',{
url:'',
abstract: true,
views: {
'': {
template: '<div ui-view=""></div>'
},
'header': {
templateUrl: 'header.html'
},
'footer': {
templateUrl: 'footer.html'
}
}
})
.state('home.index',{
url : '/',
templateUrl : 'home.html'
})
.state('home.blog',{
url : '/blog',
templateUrl : 'blog.html',
});
检查更新here