Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I typically use querystrings for filtering results, eg:

 URL:    /Events/List?type=art&city=miami&life=present

I can put them in any order and get the same results, eg:

 URL:    /Events/List?city=miami&life=present&type=art
 URL:    /Events/List?life=present&type=art&city=miami

I can also make them optional, eg:

 URL:    /Events/List?type=art, 
  or:    /Events/List?type=art&life=&city=


Question: How can I achieve this same "effect" with Angular.js routes? given the fact that parameters are passed all in a "RESTful" way in Angular

I was thinking in something like this:

 URL:   /Events/List/city/miami/life/present/type/art

Which I can achieve with a route like this:

 .when('/Events/List/city/:city?/life/:life?/type/:type?', { ... }

But I already see many problems:

  1. Order of parameters is important (I would have to define many times the route?)
  2. Optional parameters will give a non intuitive URL,
    eg: /Events/List/city/life/type/art,

    and this is not the case in optional querystrings (they are more intuitive to read I think):
    eg: /Events/List/?city=&life=&type=art
share|improve this question
    
Do you want a different route depending on the parameters? –  Joao Leal yesterday
    
mm no, I want one controller for all of those routes, just like in ASP.NET MVC I have one action that receives all those GET parameters –  sports yesterday

3 Answers 3

You can inject $routeParams into your controller. Here's an example from the docs:

// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
$routeParams ==> {chapterId:1, sectionId:2, search:'moby'}

http://docs.angularjs.org/api/ngRoute.$routeParams

share|improve this answer

I recently encountered a similar need (the ability to grab an unknown quantity of arguments off the path), and had started off by looking at $routeProvider. I ultimately abandoned that effort in favor of ui.router.

The most powerful (but possibly also tedious) approach for you would be to work with regex parameters. Consider the following (a snippet from some code I'm currently authoring):

$stateProvider
    .state("list", {
        url: "/",
        controller: "PluginsListController",
        templateUrl: "PluginsList.html"
    })
    .state("path", {
        url: "/{plugin}/*path",
        controller: "PluginDetailsController",
        templateUrl: "PluginDetails.html"
    })
;

The second state accepts two positional parameters: a plugin and a path. The path argument is a wildcard, that grabs everything after the immediately preceding slash. The idea here is that I can specify something like http://www.myfirsturl.com/app/MyFirstPlugin/level1/level2/level2.php, and end up with "MyFirstPlugin" in $stateParams["plugin"] and "level1/level2/level2.php" in $stateParams["path"]. It's up to the application logic to handle the long path parameter (and you would be likewise responsible for consuming this variable-length argument), but this approach does allow you to write a single state handler for an unknown number of url paths.

share|improve this answer

If you want to use query strings angular has $location.search() that is both a setter and getter.

The difference between angular search and window version location.search is the query falls after the url hash and when setting or getting it uses objects rather than strings

See Using $location in developer's guide

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.