Javascript 如何在angularjs上拥有多个$routeParams
我想知道我怎么能有多个路由参数,如果你们当中有人知道它的教程或答案,我会非常感激 这是我正在做的 我希望它是#/:地区/:国家/:城市 这就是控制器Javascript 如何在angularjs上拥有多个$routeParams,javascript,angularjs,Javascript,Angularjs,我想知道我怎么能有多个路由参数,如果你们当中有人知道它的教程或答案,我会非常感激 这是我正在做的 我希望它是#/:地区/:国家/:城市 这就是控制器 angular.module('NoteWrangler').controller('NotesShowController', function ($scope, $routeParams, $http){ $scope.title = $routeParams.title; $http.get('js/data
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
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: '/' } )
;
});
在控制器中:
$routeParams.region
$routeParams.country
$routeParams.city
请注意,每3个道路参数均符合以下措辞,即:
如果按此顺序有2条道路:
$routeProvider.when('/:region/:country/:city', ...
$routeProvider.when('/:user/:name/:birthdate', ...
/one/two/three => /:region/:country/:city
/12/john/1987 => /:region/:country/:city
如果将其反转:
$routeProvider.when('/:user/:name/:birthdate', ...
$routeProvider.when('/:region/:country/:city', ...
/one/two/three => /:user/:name/:birthdate
/12/john/1987 => /:user/:name/:birthdate
因此,我认为最好设置一个固定的起始路线:
$routeProvider.when('/geo/:region/:country/:city', ...
/geo/IDF/France/Paris => /geo/:region/:country/:city
[编辑]为了完成您在评论中解释的内容:
我要做的是:
$routeProvider.when(':region/:country?/:city?', ...
注意?表示参数是可选的
它将解决:
NA
NA/Mexico
NA/Mexico/Cancun
在您的控制器中,如果参数为空,请检查$routeParams,以了解您是否有一个、两个或三个参数。我不知道,可能是我问错了问题。我有一个json文件,我从中获取数据,对于每个routeparam,我需要一个不同的模板。我不知道也许我问错了问题。我有一个json文件,我从中获取数据,对于每个routeparam,我需要一个不同的模板。就像当你去/newApp/NA时,它会显示北美所有的国家,然后这个人点击并把他们带到newApp/NA/Mexico,然后显示所有的主要城市,然后在你点击一个城市newApp/NA/Mexico/Cancun之后,页面上显示的所有信息都会从json中获取,并且每个都是相互关联的。你现在明白了吗@Boris CharpentierOk你可以说有些参数是可选的吗?
NA
NA/Mexico
NA/Mexico/Cancun