Patrick Grimard
Patrick Grimard

Reputation: 7126

In AngularJS route is it possible to add default query parameters?

If this is my route setup, I'd like the "otherwise" route to redirect to '/inbound?page=0&size=20&sort=number,desc" but the "?" in the route is escaped to "%3F".

  .config(function ($routeProvider) {
    $routeProvider
      .when('/outbound', {
        templateUrl: 'views/shipments.html',
        controller: 'ShipmentCtrl'
      })
      .when('/inbound', {
        templateUrl: 'views/receipts.html',
        controller: 'ReceiptsCtrl'
      })
      .otherwise({
        redirectTo: '/inbound'
      });
  });

Upvotes: 0

Views: 74

Answers (2)

Dayan Moreno Leon
Dayan Moreno Leon

Reputation: 5435

continuing the previous answer, you should just set the default values in your inbounds route controller something like

var search=$location.search();
var page=search.page||0;
var size=search.size||20;
var sort=search.sort||'number,desc';

the exact way of doing it would depend on how permissive are you with the empty strings and the 0 size requests but thats kinda the idea

Upvotes: 1

cjmling
cjmling

Reputation: 7278

You can use $location.search() in the starting line of controller. It will add get parameter to the url.

https://docs.angularjs.org/api/ng/service/$location

Upvotes: 0

Related Questions