【问题标题】:Angular 1.4 UI Router: sub-routes?Angular 1.4 UI 路由器:子路由?
【发布时间】:2016-10-28 15:16:56
【问题描述】:

我有一个网站,其中一些页面有页眉、内容、页脚,而其他页面可能有不同的设置。该站点的每个“部分”都有自己的模块。


更新

这是当前状态的一个小插曲:
https://plnkr.co/edit/G5CdPPtULVeI1Lfa9Rjg?p=preview

您可以看到主页状态加载,并有 3 个视图加载页眉和页脚(从它们的模块文件夹)和主页内容。我正在尝试添加一个链接以将下一组视图加载到索引页面上的原始 ui 视图中,以显示带有页眉、内容和页脚视图的“示例”页面。


更新 2

具有以下提供的解决方案的 plunker:
https://plnkr.co/edit/WQHN5ehCDhKo4mkhMbgU?p=preview


原问题:

所以我正在重构我的应用程序,在根目录下有一个 ui-view,它在每个模块中加载一个子路由。我让它与默认路由一起使用,它将“home”模块加载到主视图中:

angular.module('app').config(function ($stateProvider) {
$stateProvider.state('otherwise', {
  url: '*path',
  template: '',
  controller: function ($state) {
    $state.go('home.page');
  }
});

});

home.routes.js文件是这样的:

angular.module('app').config(function ($stateProvider) {
$stateProvider.state('home', {
  url: '/home',
  templateUrl: 'modules/home/home.html',
  abstract: true,
  controller: 'HomeController as home',
  data: {
    pageTitle: 'Home Page'
  }
})
.state('home.page', {
  views: {
    header: {
      templateUrl: 'modules/header/header.html'
     },
    content: {
      templateUrl: 'modules/home/home.content.html'
     },
    footer: {
      templateUrl: 'modules/footer/footer.html'
     }
   }
  });
});

它按预期工作。抽象路由加载home.html,这只是我命名的视图:

<div ui-view="header"></div>
<div ui-view="content"></div>
<div ui-view="footer"></div>

但是我在从主页链接到 第二个 模块时遇到问题。我用与上面完全相同的设置创建了一个模块“sample”,将文件和路由从“home”复制并重命名为“sample”。

在 home.content.html 页面上,我正在尝试使用 a ui-sref="sample"&gt;Sample Section&lt;/a&gt; 创建指向第二个模块“sample”的链接,但它出错了“无法转换到抽象状态 'sample'”。

我还尝试了a ui-sref="sample.page"&gt;Sample Section&lt;/a&gt;,它不会引发任何控制台错误,但无处可去。我以为我有这个,但现在我很困惑。

sample.routes.js 的路由:

angular.module('app').config(function ($stateProvider) {
$stateProvider.state('sample', {
  url: '/sample',
  templateUrl: 'modules/sample/sample.html',
  abstract: true,
  controller: 'SampleController as sample',
  data: {
    pageTitle: 'Sample Page'
  }
})
.state('sample.page', {
  views: {
    header: {
      templateUrl: 'modules/header/header.html'
    },
    content: {
      templateUrl: 'modules/sample/sample.content.html'
     },
    footer: {
      templateUrl: 'modules/footer/footer.html'
     }
   }
  });
});

【问题讨论】:

  • 您不能转换到抽象状态(否则就是将您带到 home.page 的原因)。所以这个错误是有道理的。您必须转换到 sample.page。你的设置真的没有意义。我建议将您的所有视图(页眉、内容、页脚)移至抽象状态。
  • 当我删除“abstract=true”并将视图移动到初始状态(即“home”)时,我没有收到任何错误,而是一个空白页面。
  • 你必须粘贴你所拥有的东西让我帮忙(或者给我看一把小提琴)
  • 我的代码在上面。我根据您的建议添加了我尝试过的内容,消除了子状态并将视图移动到抽象状态“家”中。但是,如果我尝试进入“家”状态,则会收到错误“无法转换到抽象状态”。如果我从该状态删除 abstract='true',我会看到一个空白屏幕。

标签: javascript angularjs angular-ui-router


【解决方案1】:

示例如下。

我们开始吧。所以创建一个 sep 登陆页面(主页)。对于您希望在应用程序的主要部分中出现的所有状态,您可以使用名为 main 的 sep 树。如果您将第一个主要状态抽象化,从技术上讲,您可以只拥有一堆子状态。

索引页面:

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

查看次数:

<script type="text/ng-template" id="modules/main/main.content.html">
  main content
</script>

<script type="text/ng-template" id="modules/footer/footer.html">
  footer
</script>
<script type="text/ng-template" id="modules/header/header.html">
  header
</script>
<script type="text/ng-template" id="modules/home/home.content.html">
  content
</script>

<script type="text/ng-template" id="modules/home/home.html">
  Nicks {{test}}
  <a ui-sref="main">Test</a>
</script>

Javascript:

myApp.config(function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('/home');
         $stateProvider.state('home', {
    url: '/home',
    controller: 'HomeController as home',
    templateUrl: 'modules/home/home.html'
    });

  $stateProvider.state('main', {
    url: '/main',
    views: {
      'header@main': {
        templateUrl: 'modules/header/header.html'
      },
      '': {
        templateUrl: 'modules/main/main.html'
      },
      'content@main':{
        templateUrl: 'modules/main/main.content.html'
      },
      'footer@main': {
        templateUrl: 'modules/footer/footer.html'
      }
    }
  });
});

Sample fiddle

【讨论】:

  • 我原来有这个,它有效。我要做的是在索引页面上创建一个视图,然后像在子页面上一样拥有 3 个视图。这是因为某些模块没有页眉和页脚,因此我可以在这些模块上使用单个“内容”视图。我正在努力尝试在 plunkr 中复制我当前的状态
  • 然后使用一个简单的主页,然后这种类型的布局用于任何其他状态。
  • 这是一个工作(仅限主页,无法链接到其他模块)plnkr:plnkr.co/edit/G5CdPPtULVeI1Lfa9Rjg?p=preview
  • 我给了你一个你可以使用的示例设置。你要求一个不同类型的布局,你可以有一个主页,所以我添加了一个可以做到的设置。
  • 我明白你所做的,除了我的登陆页面(你有“尼克的测试”)也需要页眉和页脚视图......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多