ionic 1.X ui parameters

AngularJS. UI Router. Optional and Default Params in Route
Let's take a look at example, when we need to show list of items by categories with pagination.
Angular State Route Parameters
All Route parameters are optional by default. That's why you should use trailing slashes in case they are empty. For example, route /items/1 and /items/ will work, but not this one: /items. Let's take a closer look and pass category and page through route params:
state('items-list', {
  url: '/items-list/:cat/:page',
  templateUrl: 'items-list.html',
  controller: function($scope, $stateParams) {
     $scope.category = $stateParams.cat;
     $scope.page = $stateParams.page;
  },
  // default uri params
  params: {
    cat: 'all' ,
    page: 1
  }
});

Then, you're ready to go with links like this:
<a ui-sref="items-list({ cat: 'category-name' })">Category Name</a>

This link will navigate to /items-list/category-name/1.
Angular State Query Parameters
All query parameters are mapped to $stateParams object of UI Router. Let's pass category and page through a query parameters.
state('items-list', {
  url: '/items-list?cat&page',
  templateUrl: 'items-list.html',
  controller: function($scope, $stateParams) {
     $scope.category = $stateParams.cat || 'all';
     $scope.page = $stateParams.page || 1;
  }
});

After that, you may use such links:
<a ui-sref="items-list({ cat: 'category-name' })">Category Name</a>

Such link will navigate to /items-list?cat=category-name.
Angular Non-URL State Parameters
It is possible to pass parameters to state without showing them in the route.
state('items-list', {
  url: '/items-list',
  templateUrl: 'items-list.html',
  controller: function($scope, $stateParams) {
     $scope.category = $stateParams.cat;
  },
  params: {
    cat: 'all' //default category
  }
});

Now if you create such link:
<a ui-sref="items-list({ cat: 'category-name' })">Category Name</a>

It will navigate to /items-list. But category will be available in $stateParams.
In this case, it's not possible to share a link, but it works :-).
If you wan't to share a link with hidden parameters, you may create a button for generating link with full parameters and create a special state that will parse that link and redirect to current state with hidden parameters. Briefly that's it :-)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326479737&siteId=291194637