我想在调用子状态时访问抽象状态内的 url 参数($stateParam)。我很想知道如何做到这一点。代码在笨蛋也 http://plnkr.co/edit/L0TXx3DCBOJmh4XydXbV?p=preview
$stateProvider
.state('contacts', {
abstract: true,
//my whole point is to get the id=1 here :(
url: '/contacts/:id',
templateUrl: function($stateParams){
console.log("Did i get the child's parameter here? " + $stateParams.id)
return 'contacts' + $stateParams.id + '.html'; //this will have a ui-view in it which will render its detail.
}
})
.state('contacts.detail', {
url: '/:id',
// loaded into ui-view of parent's template
templateUrl: 'contacts.detail.html',
controller: function($scope, $stateParams){
$scope.person = $scope.contacts[$stateParams.id];
},
onEnter: function(){
console.log("enter contacts.detail");
}
})
})
`
我称之为
<a ui-sref="contacts.detail({id: 1})">My first contact<a>
我为什么要这样做?因为,我的服务器模板对于不同的联系人是不同的,布局也不同。
Well, 在 GitHub 上发布问题 https://github.com/angular-ui/ui-router/issues/1197从合作者那里得到了答案。
所以,事情就是这样。即使参数携带相同的信息,它们也必须“命名不同”。有点奇怪,但是有效。
在模板中:
<a ui-sref="contacts.detail({id: 1, duplicate_id: 1})">My first contact<a>
并在 JavaScript 中
`
$stateProvider
.state('contacts', {
abstract: true,
//my whole point is to get the id=1 here :(
url: '/contacts/:duplicate_id', //using duplicate_id here
templateUrl: function($stateParams){} //...$stateParams will have :duplicate_id key
})
.state('contacts.detail', {
url: '/:id', //using the real one here
templateUrl: function($stateParams){} //....$stateParams will have :id key
});
`
更新:我发现这种情况下的 url 重复相同的属性两次,到目前为止这对我来说没问题。欢迎任何建议。
希望这可以帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)