小编典典

Angular UI路由器嵌套视图

angularjs

我有这样的结构:

<div ui-view="main">
  <!-- content populated here by AngularJS ui-router -->
  <aside ui-view="sidebar">
    <!-- content populated here by AngularJS ui-router -->
  </aside>
</div>

我希望能够在如下所示的主要状态下定义模板,而不必在子状态下进行管理。

.state('state1', {
  url: '/',
  views: {
    'main': {
      templateUrl: '/modules/blog/partials/index.html',
      controller: 'BlogController'
    },
    'sidebar': {
      templateUrl: '/modules/core/partials/sidebar.html'
    }
  }
});

我希望命名的ui视图sidebar不是其子状态,main并由该main状态的views对象而不是子状态的templateUrl字段填充。我怎样才能做到这一点?


阅读 288

收藏
2020-07-04

共1个答案

小编典典

我们可以在 一种* 状态下使用 更多 视图,请参阅: *

该定义只需要使用绝对命名即可:

.state('state1', {
  url: '/',
  views: {
    'main': {
      templateUrl: '/modules/blog/partials/index.html',
      controller: 'BlogController'
    },
    // instead of
    // 'sidebar': {
    // we need
    'sidebar@state1': {
      templateUrl: '/modules/core/partials/sidebar.html'
    }
  }
});

如此处详细说明:

在幕后,每个视图都被分配一个遵循方案的绝对名称 viewname@statename
,其中viewname是view指令中使用的名称,状态名称是该状态的绝对名称,例如contact.item。您还可以选择以绝对语法编写视图名称。

因此,如上面的代码段所示,如果

 /modules/blog/partials/index.html

将是:

<div>
  <!-- content populated here by AngularJS ui-router -->
  <aside ui-view="sidebar">
    <!-- content populated here by AngularJS ui-router -->
  </aside>
</div>

并且index.html将包含占位符:

<div ui-view="main" ></div>

然后

  • 'main' -将在父根目录(index.html)中搜索
  • 'sidebar@state1' 将被评估为“ state1”(本身)中的viewName / target

一个
有类似想法的示例,后面有一些layout.html。

2020-07-04