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

时间:2014-10-01 09:23:18

标签: javascript angularjs state angular-ui-router

我的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 param不是必需的?我使用了经典的$ routeProvider sintax :(

2 个答案:

答案 0 :(得分:1)

尝试:

/asd/{idContact:/?.*}

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

答案 1 :(得分:1)

只想提供一些示例,给出可选参数问题的答案。在working example中查看全部内容。

本Q& A确实解释了更多细节:

以下是两个州定义的片段:

.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(尾随/