简体   繁体   English

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

[英]Angularjs UI-Router state params not working

I have a simple setup in my config() 我在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');

If i browse app/asd/7 everything works if i browse then app/asd it redirects me 如果我浏览app/asd/7一切正常,如果我浏览,则app/asd会将我重定向

I am wondering how can i make idContact param not required ? 我想知道如何使idContact参数不是必需的? I used the classic $routeProvider sintax for it :( 我用了经典的$ routeProvider sintax :(

Try: 尝试:

/asd/{idContact:/?.*}

As far as I know that is the workaround to make parameters optional in ui router. 据我所知,这是在ui路由器中使参数可选的解决方法。 I'm not sure if there have been any recent built in implementations of this in ui router. 我不确定在ui路由器中是否有任何最新的内置实现。

Just wanted to provide some example giving the answer to optional parameter issue. 只是想提供一些示例,以给出可选参数问题的答案。 See that all in working example . 工作示例中看到所有这些。

This Q&A does explain more details: 此问答确实解释了更多细节:

Here is a snippet of two states definition: 这是两个状态定义的摘要:

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

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

And here are some ways how to create links ( href or ui-sref ) to reach these states: 以下是一些方法来创建链接( 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

That should show, that we do not have to pass some param, just must be sure that the proper url is called (the trailing / ) 这表明,我们不必传递一些参数,只需确保调用了正确的url(尾随/ )即可。

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

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