7

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/#/

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: '/' } )

    ;



});

1 Answer 1

18
$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 :

If you have 2 roads in that 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 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I dont know maybe i asked the question wrong. I have json file which I'm getting the data from and for each routeparam I need a different template.
I dont know maybe i asked the question wrong. I have json file which I'm getting the data from and for each routeparam I need a different template. so like when you go /newApp/NA it shows all the countrys in northamerica then the person clicks and takes them to newApp/NA/Mexico then shows all the major cities and then after u click on a city newApp/NA/Mexico/Cancun so all the info showing up on the pages will be taken from the json and each is related to eachother. Do you understand now? @Boris Charpentier
Ok you could say that some params are optional ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.