我正在實施註冊服務,用戶建議選擇三個參數:建築(沙龍),服務和公司(類別)。對於此任務,我使用ui-router
作爲AngularJS
。什麼適當的方法來路由AngularJS中的狀態鏈?
我創建原型六個相互嵌套的狀態,像這樣:
angular.module('ngApp', ['ngAnimate', 'ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('form', {
url: '/form',
template: '<div ui-view></div>',
controller: 'dummyController'
})
.state('form.salonChoose' ,{
url:'/salChoice',
template: '<button ng-click="changeSalon()">salon 99</button>'
})
.state('form.salons', {
url: '/salons/:salonId',
template: '<div>{{model.salon}}</div>' +
'<div ui-view></div>',
controller: function($scope, $stateParams) {
$scope.model.salon = $stateParams.salonId;
location.href = '/#/form/salons/'+$scope.model.salon+'/serviceChoice';
}
})
.state('form.salons.serviceChoice', {
url:'/serviceChoice',
template: '<button ng-click="changeService()">service 99</button>'
})
.state('form.salons.services', {
url: '/services/:serviceId',
template: '<div>{{model.service}}</div>' +
'<div ui-view></div>',
controller: function($scope, $stateParams) {
$scope.model.service = $stateParams.serviceId;
location.href = '/#/form/salons/'+$scope.model.salon+'/services/'
+$scope.model.service+'/catChoice';
}
})
.state('form.salons.services.catChoice', {
url:'/catChoice',
template: '<button ng-click="changeCat()">cat 99</button>'
})
.state('form.salons.services.categories', {
url: '/categories/:catId',
template: '<div>{{model.cat}}</div>',
controller: function($scope, $stateParams) {
$scope.model.cat = $stateParams.catId;
}
});
$urlRouterProvider.otherwise('/form/salChoice');
這種方法適用於這樣的實現。簡單用例通過: 運行應用程序與自定義網址:#/form/salons/1/services/2/categories/3
它應該將在狀態form.salons.services.categories
。原型很好。
但是當我在生產應用程序中使用這種方法時,狀態在第一次改變後不會改變。不同的是,在生產嵌套狀態必須使用相同的ui-view
而不是嵌套,我認爲這是點,爲什麼國家沒有改變。
當解析URL時,什麼方法來處理狀態變化?
你試過'$ state.go()這樣做 –