我需要使用angular-ui路由器解决一个段到不同行为的路由。
例如我通过以下方式做到这一点:
$stateProvider
.state('parent', {
url: '/:route'
template: '<div ui-view/>'
controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->
# I get the type of route that the user is visiting checking server side and returning its type..
switch typeOfRoute
when "user"
$state.transitionTo('parent.user.profile')
when "place"
$state.transitionTo('parent.place.page')
else
alert("bad route")
]
})
.state('parent.user', {
abstract: true
template: '<div ui-view/>'
})
.state('parent.user.profile', {
template: '<h1>This is a profile</h1>'
controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->
# Here happens all about the user
]})
.state('parent.place', {
abstract: true
template: '<div ui-view/>'
})
.state('parent.place.page', {
template: '<h1>This is a place</h1>'
controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->
# Here happens all about the place
]})
如果有人访问,请使用上面的示例:
/ Johndoe - &gt;返回该路由是用户并转换为parent.user.profile / Newyork - &gt;返回该路线是一个地方并转换到parent.place.page
使用这种方法的问题是,当我编写路线来解决/ Johndoe /喜欢浏览器再次转到/ Johndoe时,我想考虑检查params的长度等等......但也许有人做了类似的事情,更好。
有人试图建造类似的东西吗?我坚持到这一点。
非常感谢!!
答案 0 :(得分:0)
好的,如果可以帮助某人,我最后会发布我所做的事情。
我用以下代码替换了上面的代码:
$urlRouterProvider.when('/:route', ['$match', '$stateParams', 'urlService', '$state', ($match, $stateParams, urlService, $state)->
if $match.route and $match.route.length > 0 # you can make more complex route checking here.
# here transition to the desired type route resolved server side
switch typeOfRoute
#the same for checking..
when "user"
$state.transitionTo('user.profile', $match, false)
when "place"
$state.transitionTo('place.page', $match, false)
else
return false
$stateProvider
.state('user', {
abstract: true
template: '<div ui-view/>'
})
.state('user.profile', {
template: '<h1>This is a profile</h1>'
controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->
# Here happens all about the user
]})
.state('place', {
abstract: true
template: '<div ui-view/>'
})
.state('place.page', {
template: '<h1>This is a place</h1>'
controller: ['$scope', '$state', '$stateParams', ($scope, $state, $stateParams)->
# Here happens all about the place
]})