【问题标题】:Angular-ui ui-router - using multiple nested viewsAngular-ui ui-router - 使用多个嵌套视图
【发布时间】:2014-05-18 07:59:02
【问题描述】:

我正在尝试 ui-router 插件的嵌套视图功能,但遇到了我不知道如何解决的问题。 显示问题的代码可以在http://jsfiddle.net/3c9h7/1/ 找到:

  var app = angular.module('myApp', ['ui.router']);

  app.config(function($stateProvider) {
    return $stateProvider.state('root', {
      template: "<div class='top' ui-view='viewA'></div><div class='middle' ui-view='viewB'></div>"
    }).state('root.step1', {
      url: '',
      views: {
        'viewA': {
          template: '<h1>View A</h1>'
        }
      }
    }).state('root.step1.step2', {
      url: '/step2',
      views: {
        'viewB': {
          template: '<h1>View B</h1>'
        }
      }
    });
  });

  app.controller('MainCtrl', [
    '$scope', '$state', function($scope, $state) {
      $state.transitionTo('root.step1.step2');
    }
  ]);

<div ng-app='myApp' ui-view ng-controller='MainCtrl'>
</div>

因此,代码通过使用 $state.go 方法激活“root.step1.step2”状态(https://github.com/angular-ui/ui-router/wiki/Quick-Reference#stategoto--toparams--options) 根据 ui-router 文档:

当应用程序处于特定状态时 - 当状态处于 “活跃”——它的所有祖先状态也是隐式活跃的。

因此,我希望“root.step1”和“root”将处于活动状态并且按预期工作,但“viewB”没有填充模板,正如您在 jsfiddle 示例中看到的那样:顶部 viewA(root. step1) 可以,但是中间的viewB(root.step1.step2) 是空的。 我做错了什么?

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    文档说:

    子状态会将其模板加载到其父级的 ui 视图中。

    所以 viewA 模板内应该有一个ui-view='viewB',因为root.step1.step2 的父状态是root.step1。或者viewB应该是root.step1的view之一,应该没有root.step1.step2

    【讨论】:

    • 非常感谢。我能够通过在 root.step1.step2 状态下使用绝对视图名称 viewB@root 来解决问题:jsfiddle.net/3c9h7/2 但是您的建议确实有助于解决这个问题。