ui 路由器不把内容放在命名的 ui-view 中 [英] ui router does not put the content in the named ui-view

查看:20
本文介绍了ui 路由器不把内容放在命名的 ui-view 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我进入路线

\#\projects\100\dates\2014-01-01

在 url 中按回车键,我得到项目"状态.

我希望触发projects.selected.dates"状态.

为什么路由不起作用?实际上它在我的机器上本地工作,没有命名视图......

http://plnkr.co/edit/0DJ6W7QEPx2UzpdzDrVu?p=preview

'use strict';有角的.module('projectplanner', ['ui.router']).config(function($stateProvider, $urlRouterProvider) {$urlRouterProvider.otherwise('/projects');$stateProvider.state('项目', {网址:'/项目',意见:{'菜单': {模板:开始你的项目!"},'内容': {templateUrl: "projects.html",控制器:'项目控制器'}}}).state('projects.selected', {url: '/:projectId'}).state('projects.selected.dates', {url: '/dates/:date',意见:{'菜单': {templateUrl: 'menu.html'},'内容': {templateUrl: 'dateplanner.html',控制器:'DateplannerController'}}})});<!DOCTYPE html><html ng-app="projectplanner"><头><meta charset="utf-8"/><title>AngularJS Plunker</title><脚本>document.write('<base href="' + document.location + '"/>');<link rel="stylesheet" href="style.css"/><script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.20/angular.js" data-semver="1.2.20"></脚本><script data-require="ui-router@*" data-semver="0.2.10" src="https://rawgit.com/angular-ui/ui-router/0.2.10/release/angular-ui-router.js"></script><script src="app.js"></script><script src="ProjectsController.js"></script><script src="DateplannerController.js"></script><身体><div class="wrapper" ><header class="aside">标志</header><div ui-view="menu" id="menu" class="aside"></div><div ui-view="content" class="main">

</html>

解决方案

有更新和有效的 plunker,这是必不可少的更新:

 .state('projects.selected.dates', {url: '/dates/:date',意见:{'menu@': {//这里添加了@templateUrl: 'menu.html'},'content@': {//这里添加了@templateUrl: 'dateplanner.html',控制器:'DateplannerController'}}})

查看ui-view名称末尾的'@'

完整的解释在这里:

<块引用>

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

所以,我所做的是使用绝对名称 .. 定位根目录 - index.html

文档中的一些示例...很好地解释了所有内容:

$stateProvider.state('联系人', {//这将自动插入未命名的 ui-view//父状态模板.由于这是一个顶级状态,//它的父状态模板是 index.html.templateUrl: 'contacts.html'}).state('contacts.detail', {意见:{///////////////////////////////////////相对定位////以父状态 ui-view 为目标/////////////////////////////////////////相对地针对此状态的父状态联系人"中的详细信息"视图.//<div ui-view='detail'/>在contacts.html中细节" : { },//相对地指向此状态的父状态中的未命名视图,'contacts'.//

在contacts.html中"" : { },////////////////////////////////////////////////////////使用@"的绝对定位////定位此状态内的任何视图或祖先//////////////////////////////////////////////////////////绝对针对此状态下的 'info' 视图,'contacts.detail'.//<div ui-view='info'/>在contacts.detail.html 中info@contacts.detail":{}//绝对以联系人"状态下的详细信息"视图为目标.//<div ui-view='detail'/>在contacts.html中详细@联系人":{}//绝对以父联系人"状态的未命名视图为目标.//

在contacts.html中@联系人":{}

When I enter the route

\#\projects\100\dates\2014-01-01

in the url and press return I get the "projects" state.

I expected to trigger "projects.selected.dates" state.

Why does the routing not work? Actually it works locally on my machine without named view...

http://plnkr.co/edit/0DJ6W7QEPx2UzpdzDrVu?p=preview

'use strict';
angular
  .module('projectplanner', ['ui.router'])
  .config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/projects');
    $stateProvider
      .state('projects', {

        url: '/projects',
        views: {
          'menu': {
            template: 'Start your projects!'
          },
          'content': {
            templateUrl: "projects.html",
            controller: 'ProjectsController'
          }
        }
      })
      .state('projects.selected', {
        url: '/:projectId'
      })
      .state('projects.selected.dates', {
        url: '/dates/:date',
        views: {
          'menu': {
            templateUrl: 'menu.html'
          },
          'content': {
            templateUrl: 'dateplanner.html',
            controller: 'DateplannerController'
          }
        }
      })
  });

<!DOCTYPE html>
<html ng-app="projectplanner">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title> 
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
 <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.20/angular.js" data-semver="1.2.20"></script>
  <script data-require="ui-router@*" data-semver="0.2.10" src="https://rawgit.com/angular-ui/ui-router/0.2.10/release/angular-ui-router.js"></script>

  <script src="app.js"></script>
  <script src="ProjectsController.js"></script> 
    <script src="DateplannerController.js"></script> 

</head>

<body>
  <div class="wrapper" >
    <header class="aside">Logo</header>
    <div ui-view="menu" id="menu" class="aside"></div>
    <div ui-view="content" class="main">
    </div>
  </div>
</body>

</html>

解决方案

There is the updated and working plunker, and this is the essential update:

  .state('projects.selected.dates', {
    url: '/dates/:date',
    views: {
      'menu@': {                     // HERE is @ added
        templateUrl: 'menu.html'
      },
      'content@': {                  // HERE is @ added
        templateUrl: 'dateplanner.html',
        controller: 'DateplannerController'
      }
    }
  })

See the '@' at the end of the ui-view name

The full explanation is here:

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.

So, what I did is use of the absolute name .. targeting the root - index.html

Some examples from documentation... greatly explaining that all:

$stateProvider
  .state('contacts', {
    // This will get automatically plugged into the unnamed ui-view 
    // of the parent state template. Since this is a top level state, 
    // its parent state template is index.html.
    templateUrl: 'contacts.html'   
  })
  .state('contacts.detail', {
    views: {
        ////////////////////////////////////
        // Relative Targeting             //
        // Targets parent state ui-view's //
        ////////////////////////////////////

        // Relatively targets the 'detail' view in this state's parent state, 'contacts'.
        // <div ui-view='detail'/> within contacts.html
        "detail" : { },            

        // Relatively targets the unnamed view in this state's parent state, 'contacts'.
        // <div ui-view/> within contacts.html
        "" : { }, 

        ///////////////////////////////////////////////////////
        // Absolute Targeting using '@'                      //
        // Targets any view within this state or an ancestor //
        ///////////////////////////////////////////////////////

        // Absolutely targets the 'info' view in this state, 'contacts.detail'.
        // <div ui-view='info'/> within contacts.detail.html
        "info@contacts.detail" : { }

        // Absolutely targets the 'detail' view in the 'contacts' state.
        // <div ui-view='detail'/> within contacts.html
        "detail@contacts" : { }

        // Absolutely targets the unnamed view in parent 'contacts' state.
        // <div ui-view/> within contacts.html
        "@contacts" : { }

这篇关于ui 路由器不把内容放在命名的 ui-view 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆