容器视图和默认视图

容器视图和默认视图

本文介绍了Angular ui-router:容器视图和默认视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力为下一个状态创建一个容器,将状态定义为视图,分为页眉、容器、页脚.

I am struggling to create a container for next states, defined the states as views, divided into header, CONTAINER, footer.

以博客为例,下一个状态是博客,但我没有看到将其放入视图的方法.

The next state as an example would be the blogs, but I do not see a way of getting it into the view.

一个想法是将主页视图作为标准启动,但也失败了.

One idea was to start the HOME view as standard, but also failed.

查看:

<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

Plunker Example: http://plnkr.co/edit/yRgqiAeEVQl2WVajGgG0?p=preview

推荐答案

在上面更新的问题中,您使用了 这个plunker 展示你是如何让它工作的:

In the updated question above, you've used this plunker to show how you made it working:

.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',
});

虽然该解决方案有效,但实际上不是您/我们应该采用的方式.因为父状态'home',注入到未命名视图itslef - templateUrl: 'index.html'

While that solution is working, it is in fact not a way you/we should go. Because the parent state 'home', injects into unnamed view itslef - templateUrl: 'index.html'

所以,现在又有视图headerfooter,但它们确实不同于根(原始index.htm).它们的绝对名称将是 'header@home' 和 'footer@home' (在代码片段中使用) - 一切似乎都在工作.

So, now there are again views header and footer, but they do differ from the root (original index.htm). Their absolute name would be 'header@home' and 'footer@home' (as used int the code snippet) - and all seems to be working.

但那是多余的.除非我们将布局移动到某种布局"状态和layout.html"

But that is redundant. Unless we will move the layout into some 'layout' state and 'layout.html'

为什么是多余的?因为 index.html 已经在使用 (作为 root) 并且它包含这些目标.它们的绝对名称是header@"和footer@".这应该是要走的路.

Why redundant? Because index.html already is in play (as a root) and it contains these targets. their absolute name is 'header@' and 'footer@'. And that should be the way to go.

为了清楚起见,有一个更新的plunker及其片段:

To make it clear, there is an updated plunker and its snippets:

.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',
});

此处

这篇关于Angular ui-router:容器视图和默认视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:42