How to have multiple $ routeParams on angularjs

I wanted to know how I can have several route parameters, and if any of you guys can know the tutorial or the answer, I would really appreciate it.

here is what works on http://jaysg.com/newApp/#/

I want it to be # /: 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

 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: '/' } ) ; }); 
+6
source share
1 answer
 $routeProvider.when('/:region/:country/:city', ... 

And in the controller:

 $routeParams.region $routeParams.country $routeParams.city 

Beware that each of the three parameters of the road corresponds to this formulation, which means:

If you have 2 roads in this order:

 $routeProvider.when('/:region/:country/:city', ... $routeProvider.when('/:user/:name/:birthdate', ... /one/two/three => /:region/:country/:city /12/john/1987 => /:region/:country/:city 

If you invert it:

 $routeProvider.when('/:user/:name/:birthdate', ... $routeProvider.when('/:region/:country/:city', ... /one/two/three => /:user/:name/:birthdate /12/john/1987 => /:user/:name/:birthdate 

Therefore, I believe that it is best to set a fixed start route:

 $routeProvider.when('/geo/:region/:country/:city', ... /geo/IDF/France/Paris => /geo/:region/:country/:city 

[EDIT] To do what you explain in the comment:

What would I do:

 $routeProvider.when(':region/:country?/:city?', ... 

Pay attention to ?, this means that the parameter is optional.

He will allow:

 NA NA/Mexico NA/Mexico/Cancun 

And you have a controller with your $ routeParams if the parameter is zero, if you have two or three parameters.

+15
source

Source: https://habr.com/ru/post/983777/


All Articles