Angular ui-router : Parent &子视图 [英] Angular ui-router : Parent & Child Views

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

问题描述

我想建立一个具有以下结构的站点 header-view, main-view, footer-view .所以我定义了一个包含标头 & 的根路由.页脚.root 的子级将成为我的所有站点.在这些站点中,我将拥有更多嵌套视图.

I want to build a site with the following structure header-view, main-view, footer-view . So I defined a root route which contains the header & footer. Children of root will be all my sites.Within these sites I will have more nested views.

在下面的代码中,它确实显示了页眉,但不显示页脚 &主要观点.一旦我删除了父继承,它就会显示主视图而不是标题 &页脚.

In the code below it does show the header, but not the footer & the main view. As soon as I remove the parent inheritance, it shows the main view but not the header & the footer.

HTML

<body ng-app="App">
    <header ui-view="header"></header>
    <main ui-view></ui-view>
    <footer ui-view="footer"></footer>
</body>

JS

   module.config(function($urlRouterProvider, $stateProvider) {
      $urlRouterProvider.otherwise('/home');
      $stateProvider
        .state('root', {
          abstract: true,
          views: {
            '@': {
                controller: 'RootCtrl',
                controllerAs: 'rootCtrl'
            },
            'header@': {
                templateUrl: 'modules/header/header.html',
                controller: 'HeaderCtrl',
                controllerAs: 'headerCtrl'
            },
            'footer@': {
                templateUrl: 'modules/footer/footer.html',
                controller: 'FooterCtrl',
                controllerAs: 'footerCtrl'
                }
           }
        })
        .state('root.home',{
            parent:'root',
            url:'',
            templateUrl:'modules/home/home.html',
            controller: 'HomeController',
            controllerAs:'homeCtrl'
        });
    });

推荐答案

有一个链接到 workingplunker.

UI-Router 如何为视图找到目标/锚点的逻辑是:总是尝试将 child 插入 parent.如果不可能,则使用绝对命名.见:

The UI-Router logic how to find a target/anchor for a view is: always try insert child into parent. If not possible, then use absolute naming. see:

所以我改变的是,父未命名视图现在包含子视图的目标/锚点 - 未命名视图 <ui-view/>:

So what I changed, is, that the parent unnamed view is now containing a target/anchor for a child - unnamed view <ui-view />:

.state('root', {
  abstract: true,
  views: {
    '@': {
        template: '<ui-view />', // NEW line, with a target for a child
        controller: 'RootCtrl',
        controllerAs: 'rootCtrl'
    },
    ....

另外,因为我们说,默认网址是

Also, because we say, that the default url is

  $urlRouterProvider.otherwise('/home');

我们必须有这样的状态:

we have to have such state:

.state('root.home',{
    parent:'root',
    url:'/home', // this is the otherwise, to have something on a start up

此处

另一种使用 plunker 的方法,可以是将根视图定位到孩子:

Another approach with a plunker here, could be to target the root view in the child:

.state('root.home',{
    parent:'root',
    url:'/home',
    views: {
      '@': {
      templateUrl:'home.html',
      controller: 'HomeController',
      controllerAs:'homeCtrl'
      }
    },

在这种情况下,父状态不必具有 <ui-view/> - 我们以 root 为目标,但我们不会继承任何东西......为什么会这样?请参阅此链接:

In this case, the parent state does not have to have <ui-view /> - we target root, but we won't be inheriting anything... Why is that? See this link:

这篇关于Angular ui-router : Parent &amp;子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆