我不知道如果我这样做的方式是正确的,任何意见将不胜感激。如何重定向到状态,如果特定的stateParam为空
我有一个餐厅选择器,用户可以从中选择一家餐馆。然后,所有其他的孩子状态都会加载特定于所选餐厅的内容。但是我需要默认选择一个孩子状态,包括一个餐厅,根据用户最近的位置选择一个餐厅,如果他们之前选择了一个,则选择cookie数据。但是,我不知道如何在默认餐馆ID的情况下重定向到儿童状态?
我的代码的一个混蛋版本下面来说明。
app.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/restaurant/[some id based on cookie info]/our-food"); // if no id is set, then I want to get it from a cookie, but then how do i do the redirect?
$stateProvider
.state('restaurant', {
url: '/restaurant/:restaurantId',
template: "<ui-view/>",
controller: function($state, $stateParams, Data, RestaurantService, $cookieStore) {
if(typeof $stateParams.restaurantId !== 'undefined') {
// get the restaurantID from the cookie
}
}
})
.state('restaurant.our-food', {
url: '/our-food',
templateUrl: function($stateParams) {
return 'templates/food-food.html?restaurantId=' + $stateParams.restaurantId;
},
controller: 'SubNavCtrl'
})
.state('restaurant.glutenfree-vegetarian', {
url: '/glutenfree-vegetarian',
templateUrl: function($stateParams) {
return 'templates/food-vegetarian.html?restaurantId=' + $stateParams.restaurantId;
},
controller: 'SubNavCtrl'
})
下面的图像来说明什么是在前端发生的事情: www.merrywidowswine.com/ss.jpg
谢谢,这确实指向了我正确的方向,我将会有一个戏剧,看看我是否能够完成所有工作。干杯! –
你的#1解决方案的问题(我有)是所有解析属性在执行'onEnter'前解析。我在github上开始了一个问题:https://github.com/angular-ui/ui-router/issues/1102。如果您不希望在重定向之前解析已解析的参数(例如,如果路由需要特定参数),我不确定是否存在隐藏的短路。 – Beez
而不是'$ state.transitionTo'可能会更好地使用'$ state.go',正如[here]所述(https://github.com/angular-ui/ui-router/wiki/Quick-Reference#状态-1) – Daniele