[英]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'
})
以下是一些方法來創建鏈接( href
或ui-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.