2014-10-01 74 views
1

我有一个简单的设置在我的配置()Angularjs UI-路由器状态PARAMS不工作

$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它重定向我

我想知道如何app/asd/7一切正常我可以使idContact参数不是必需的吗?我用了经典的$ routeProvider sintax它:(

+0

幸运的猜测:“app/asd /”是否带有尾部斜线工作? – jevgenig 2014-10-01 09:26:23

+0

@jevgenig我在控制台中得到这个错误**由于: 错误:模式'/ app/asd /:idContact?'中无效的参数名称'**不能理解它有什么问题 – sbaaaang 2014-10-01 09:27:24

+0

“ :varName的“?语法似乎不受stateProvider支持,但是routerProvider。尝试使用正则表达式格式:'/ asd [/ I:idContact]' – jevgenig 2014-10-01 09:32:51

回答

1

尝试:

/asd/{idContact:/?.*}

据我知道,就是让参数UI路由器可选的解决方法,我不知道是否有。已经任何最近建成的这个实现在UI路由器。

+0

是啊谢谢我想要自杀,谢谢:D – sbaaaang 2014-10-01 09:45:05

1

只是想提供一些例子给答案可选参数的问题。检查所有在working example

这个问答& 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被调用(尾随/

+0

赞赏谢谢 – sbaaaang 2014-10-08 16:36:19

+0

伟大的,如果这可以帮助无论如何;) – 2014-10-08 16:36:37

+0

肯定谢谢你! – sbaaaang 2014-10-08 16:37:16