Jay*_*ger 7 javascript angularjs
我想知道我怎么能有多个路线参数,如果你们中的任何一个人可能知道它的教程或答案我会非常感激它在这一点上.
这是我在http://jaysg.com/newApp/#/上工作的内容
我希望它是#/:region /:country /:city
所以这是控制器
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];
});
});
Run Code Online (Sandbox Code Playgroud)
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: '/' } )
;
});
Run Code Online (Sandbox Code Playgroud)
Bor*_*ier 17
$routeProvider.when('/:region/:country/:city', ...
Run Code Online (Sandbox Code Playgroud)
在控制器中:
$routeParams.region
$routeParams.country
$routeParams.city
Run Code Online (Sandbox Code Playgroud)
请注意,每个3参数的道路都是这样的措辞,意思是:
如果您按顺序有2条道路:
$routeProvider.when('/:region/:country/:city', ...
$routeProvider.when('/:user/:name/:birthdate', ...
/one/two/three => /:region/:country/:city
/12/john/1987 => /:region/:country/:city
Run Code Online (Sandbox Code Playgroud)
如果你颠倒它:
$routeProvider.when('/:user/:name/:birthdate', ...
$routeProvider.when('/:region/:country/:city', ...
/one/two/three => /:user/:name/:birthdate
/12/john/1987 => /:user/:name/:birthdate
Run Code Online (Sandbox Code Playgroud)
所以我认为最好采用固定的起跑路线:
$routeProvider.when('/geo/:region/:country/:city', ...
/geo/IDF/France/Paris => /geo/:region/:country/:city
Run Code Online (Sandbox Code Playgroud)
[编辑]为了做你在评论中解释的内容:
我会做的是:
$routeProvider.when(':region/:country?/:city?', ...
Run Code Online (Sandbox Code Playgroud)
注意?,这意味着param是可选的.
它将解决:
NA
NA/Mexico
NA/Mexico/Cancun
Run Code Online (Sandbox Code Playgroud)
并且在你的控制器中检查你的$ routeParams如果param为null则知道你是否有一个两个或三个参数.