javascript - Angular UI 路由器嵌套 View

标签 javascript angularjs angular-ui angular-ui-router

我有这样的结构:

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

我希望名为 sidebar 的 ui-view 不是 main 的子状态,而是由 main 状态的 View 填充对象而不是子状态的 templateUrl 字段。我该怎么做?

最佳答案

我们可以在一个状态下使用更多 View ,参见:

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

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

如这里的详细解释:

Behind the scenes, every view gets assigned an absolute name that follows a scheme of viewname@statename, where viewname is the name used in the view directive and state name is the state's absolute name, e.g. contact.item. You can also choose to write your view names in the absolute syntax.

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

的内容
 /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

example有类似的想法和一些 layout.html 背后......

关于javascript - Angular UI 路由器嵌套 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587280/

相关文章:

jquery - 将 jQuery 插件输入值推送到 AngularJS 中的模型

javascript - 使用javascript删除最后一个html元素

javascript - 根据填充的必填字段构建和操作数组

javascript - node.js,setTimeout回调方法和 "this"

javascript - 无法删除 ui-gmap-google-map 中的选定多边形

jquery-ui - AngularJS 嵌套模式对话框

javascript - 我如何在 JavaScript 中监听三次点击?

javascript - AngularJS:通过插值观察嵌入的内容变化?

javascript - mootools 1.4.2 和 angular 1.3 在 ie8 中一起玩得很好

javascript - Angular 用户界面选择 : Change theme