javascript 如何在 angularjs 上拥有多个 $routeParams
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29044472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to have multiple $routeParams on angularjs
提问by Jayswager
I wanted to know how can i have multiple route params and if any of you guys might know a tutorial to it or the answer i would appreciate it so much at this point.
我想知道我怎么能有多个路由参数,如果你们中的任何一个人可能知道它的教程或答案,我会在这一点上非常感激。
here's what im working on http://jaysg.com/newApp/#/
这是我在http://jaysg.com/newApp/#/ 上的工作
I want it to be #/:region/:country/:city
我希望它是 #/:region/:country/:city
so this is the controller
所以这是控制器
angular.module('NoteWrangler').controller('NotesShowController', function ($scope, $routeParams, $http){
$scope.title = $routeParams.title;
$http.get('js/data/notes.json').success(function(data) {
$scope.note = data.filter(function(entry){
return entry.title === $scope.title;
})[0];
});
});
routes.js
路由.js
angular.module('NoteWrangler')
.config(function($routeProvider){
$routeProvider.when('/notes', {
templateUrl: 'templates/pages/notes/index.html',
controller: 'NotesIndexController',
controllerAs: 'indexController'
})
.when('/users', {
templateUrl: 'templates/pages/users/index.html',
})
.when('/', {
templateUrl: 'templates/pages/notes/index.html',
controller: 'NotesIndexController',
controllerAs: 'indexController'
})
.when('/:countryName', {
templateUrl: 'templates/pages/notes/country-detail.html',
controller: 'CountryDetailCtrl'
})
.when('/notes/:title', {
templateUrl: 'templates/pages/notes/show.html',
controller: 'NotesShowController',
controllerAs: 'showController'
})
.otherwise( { redirectTo: '/' } )
;
});
回答by Boris Charpentier
$routeProvider.when('/:region/:country/:city', ...
And in controller :
在控制器中:
$routeParams.region
$routeParams.country
$routeParams.city
Beware, that every 3 parameters road are true with this wording, meaning :
请注意,每 3 个参数路径对于此措辞都是正确的,意思是:
If you have 2 roads in that order:
如果您按该顺序有 2 条道路:
$routeProvider.when('/:region/:country/:city', ...
$routeProvider.when('/:user/:name/:birthdate', ...
/one/two/three => /:region/:country/:city
/12/john/1987 => /:region/:country/:city
If you inverse it :
如果你反转它:
$routeProvider.when('/:user/:name/:birthdate', ...
$routeProvider.when('/:region/:country/:city', ...
/one/two/three => /:user/:name/:birthdate
/12/john/1987 => /:user/:name/:birthdate
So I think it's best to put a fixed starting route :
所以我认为最好设置一个固定的出发路线:
$routeProvider.when('/geo/:region/:country/:city', ...
/geo/IDF/France/Paris => /geo/:region/:country/:city
[EDIT] In order to do what you explain in comment :
[编辑] 为了做你在评论中解释的内容:
What I would do is :
我会做的是:
$routeProvider.when(':region/:country?/:city?', ...
note the ?, it means that the param is optional.
注意 ?,这意味着参数是可选的。
It will resolve :
它将解决:
NA
NA/Mexico
NA/Mexico/Cancun
And in you controller check with your $routeParams if the param is null to know if you have one two or three params.
并且在您的控制器中检查您的 $routeParams 参数是否为空,以了解您是否有两个或三个参数。