此場景與ui-router
可能有兩個(如果不是更多)解決方案。請在this working example這裏檢查它們。我們可以先繼續使用您的括號,孩子的情況下,我們只需要做一些改變
// Parent - Child Scenario
.config(['$stateProvider',
function($stateProvider) {
// parent child
$stateProvider
.state('shoppings-view', {
url: '/shoppings/:id',
templateUrl: 'tpl.shoppings-view.html',
controller: 'ParentCtrl',
})
.state('shoppings-view.order', {
url: '/:order',
template: '<div>some child content if needed</div>',
controller: 'ChildCtrl',
});
}])
.controller('ParentCtrl', function($scope, $state, $stateParams, DataSvc) {
$scope.data = [];
// parent declares method on a $scope
$scope.load = function(params){
$scope.data = DataSvc.getAll(params)
}
$scope.load($stateParams);
})
.controller('ChildCtrl', function($scope, $state, $stateParams) {
// child scope has inherit that function
// and calls the parent to relaod with new params
$scope.load($stateParams);
})
我們可以看到這裏,是孩子得到$範圍繼承(見Understanding Scopes),因此可以訪問父方法$scope.load($stateParams);
。每當新的子狀態被新的參數調用時,它就會調用父進程來重新傳遞數據。
也許不是這裏最好的,但對父母$範圍公開的方法,可用於子女的概念,是一個功能我使用了很多...
第二條本辦法可能是將所有這些東西變成一個簡單的狀態,更多的PARAMS:
// scenario with Single state
.config(['$stateProvider',
function($stateProvider) {
// single state
$stateProvider
.state('shoppings', {
url: '/shoppings-single/:id/:order',
templateUrl: 'tpl.shoppings-single.html',
controller: 'SingleCtrl',
resolve : {
data : function($stateParams, DataSvc){
return DataSvc.getAll($stateParams)
}
}
});
}])
.controller('SingleCtrl', function($scope, $state, $stateParams, data) {
$scope.data = data;
$scope.orderBy = $stateParams.order;
})
沒有什麼特別的,只是我們可以看到,一個國家能有更多的PARAMS(見URL Parameters)
所有一起檢查here
謝謝。偉大的人,現在我可以更好地理解國家的使用。 – Scoup
很高興看到;)享受鑽石'ui路由器... ... –
這個答案的第一部分過時了嗎? ui-router文檔會說「沒有其他東西是繼承的(沒有控制器,模板,網址等)」。 https://github.com/angular-ui/ui-router/wiki/Nested-States-and-Nested-Views#what-do-child-states-inherit-from-parent-states –