繁体   English   中英

AngularJS UI-Router状态参数不起作用

[英]Angularjs UI-Router state params not working

我在config()中有一个简单的设置

    $stateProvider
        .state('app', {
          'url': '/app',
          'abstract': true,
          'templateUrl': 'views/layout/index.html',
          'controller': 'App'
        })

        .state('app.home', {
          'url': '/home',
          'views': {
            'appContent': {
              'templateUrl': 'views/home/index.html',
              'controller': 'Home'
            }
          }
        });
        .state('app.asd', {
          'url': '/asd/:idContact?',
          'views': {
            'appContent': {
              'templateUrl': 'views/asd/index.html',
              'controller': 'Asd'
            }
          }
        });
$urlRouterProvider.otherwise('/app/home');

如果我浏览app/asd/7一切正常,如果我浏览,则app/asd会将我重定向

我想知道如何使idContact参数不是必需的? 我用了经典的$ routeProvider sintax :(

尝试:

/asd/{idContact:/?.*}

据我所知,这是在ui路由器中使参数可选的解决方法。 我不确定在ui路由器中是否有任何最新的内置实现。

只是想提供一些示例,以给出可选参数问题的答案。 工作示例中看到所有这些。

此问答确实解释了更多细节:

这是两个状态定义的摘要:

.state('view', {
    url: '/view/:inboxId',
    templateUrl: 'tpl.view.html',
    controller: 'viewCtrl'
}).

state('view_root', {
    url: '/view',
    templateUrl: 'tpl.view.html',
    controller: 'viewCtrl'
})

以下是一些方法来创建链接( hrefui-sref )以达到这些状态:

// href
<a href="#/view/1">/view/1</a> - id is passed<br />
<a href="#/view/"> /view/ </a> - is is missing - OPTIONAL like <br />
<a href="#/view">  /view  </a> - url matching the view_root

// ui-sref
<a ui-sref="view({inboxId:2})">    - id is passed<br /> 
<a ui-sref="view">                 - is is missing - OPTIONAL like
<a ui-sref="view({inboxId:null})"> - id is explicit NULL <br />
<a ui-sref="view_root()">          - url matching the view_root

这表明,我们不必传递一些参数,只需确保调用了正确的url(尾随/ )即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM