HOWTO · Angular

How to Use Angular stateParams

Learn how UI-Router state parameters work in AngularJS: define URL parameters, read them with $stateParams, pass values during navigation, and diagnose unexpected values.

On this page

$stateParams is a UI-Router service that exposes the parameter values associated with the active state. A state declares which values belong to its URL, and navigation supplies values for those parameters. In an AngularJS controller, you can read a value such as id from $stateParams and make it available to the state’s view.

This is useful when a destination state needs an identifier or other small value from the route. The URL pattern, the destination state’s name, and the object passed to $state.go work together; $stateParams provides the resulting values to the active state.

What $stateParams Contains

$stateParams is an object whose properties correspond to parameters available to the active UI-Router state. For a state URL such as /signup/:id, the parameter name is id; navigating to that state with an id value makes it available as $stateParams.id in the destination state. The parameter is a route value, not a full user record. An application can use it to select data separately, but data loading is outside this parameter-passing example.

Use the exact parameter name declared by the URL pattern. If the state declares :id, reading $stateParams.userId will not read that parameter. Values can also be supplied through a transition, so check both the state definition and the navigation call when tracing where a value comes from.

Define URL Parameters in a UI-Router State

States are configured through $stateProvider. A state’s url property defines its URL pattern; a colon introduces a named parameter. For example, the state configuration can include url: '/signup/:id'. This declares a state named signup with an id parameter. Properties such as template and controller are also part of the state configuration, rather than arguments to a state function.

The parameter name in the URL and the key used during navigation must agree. A pattern like /signup/:id pairs with a navigation object containing id, as in $state.go('signup', { id: '2nd Parameter' }). The router then makes that value available to the active state’s parameter service.

Read Parameters in a Controller or Template

Inject $stateParams into the destination state’s controller and copy the value to the scope if the template needs it. For the id parameter, the essential controller assignment is $scope.id = $stateParams.id;. The template can then display it with AngularJS interpolation: {{ id }}. This keeps the controller responsible for exposing the value and the view responsible for presenting it.

You can also read $stateParams.id directly where the application is already using the service, but keeping the value on the scope makes the controller-to-template relationship explicit. Do not assume that a parameter automatically retrieves a database record; it supplies the route value, and application code decides what to do with it.

Pass Parameters When Navigating Between States

Use $state.go with the destination state’s name and an object containing its parameter values. For the example pattern /signup/:id, navigation is $state.go('signup', { id: '2nd Parameter' }). The first argument identifies the destination state; the second supplies values keyed by the parameter names in that state’s URL.

When navigation originates in a template, the same state transition can be invoked by the controller or through the UI-Router directive configured for that view. Regardless of the trigger, the destination state must declare the parameter and the supplied key must match it. A mismatch between id and another key such as userId is a common reason a value is absent or unexpected.

Run a Complete Two-State Example

The home state can navigate to signup with an id value. The destination declares that parameter in /signup/:id, reads it through $stateParams.id, and exposes it to the view as $scope.id. This compact AngularJS/UI-Router sketch shows how the pieces connect; it is illustrative and is not presented as a runnable application:

<div ng-app="exampleApp">
  <div ui-view></div>

  <script>
    angular.module('exampleApp', ['ui.router'])
      .config(function ($stateProvider) {
        $stateProvider
          .state('home', {
            url: '/home',
            template: '<button ng-click="openSignup()">Sign up</button>',
            controller: function ($scope, $state) {
              $scope.openSignup = function () {
                $state.go('signup', { id: '2nd Parameter' });
              };
            }
          })
          .state('signup', {
            url: '/signup/:id',
            template: '<h1>SignUp State</h1><p>{{ id }}</p>',
            controller: function ($scope, $stateParams) {
              $scope.id = $stateParams.id;
            }
          });
      });
  </script>
</div>

The flow is explicit: home calls $state.go('signup', { id: '2nd Parameter' }); the signup URL pattern names the parameter :id; its controller reads $stateParams.id; and its template displays the exposed id value.

For a separate value-shape check, this Python example models the parameter object and prints the value associated with its id key. It does not execute AngularJS or UI-Router.

Run this example:

state_params = {"id": "2nd Parameter"}
print(state_params["id"])
2nd Parameter

The supplied browser image shows a SignUp State view with $stateParams displaying the id value 2nd Parameter. It is a visual reference for the destination state; the selectable code and output above show the value separately.

Browser view of the SignUp State displaying $stateParams with id set to “2nd Parameter”

The destination view displays the parameter value supplied during navigation.

Diagnose Missing or Unexpected Parameters

If the destination reads no value, check these items in order:

  1. Confirm the active destination state is the one that declares the parameter.
  2. Compare the parameter name in its URL pattern, such as :id, with the property read by the controller, such as $stateParams.id.
  3. Check that the navigation targets the same state name and supplies the matching key, for example $state.go('signup', { id: value }).
  4. Inspect the value at the transition call and in the destination controller to find where it changes.

If the value appears under an unexpected name, align the URL parameter, navigation object key, controller property, and template expression. If a parameter is optional in the application’s route design, handle its absence in the controller before using it; do not treat $stateParams as a guarantee that every property has a value. These checks isolate whether the issue is state selection, parameter naming, or the value supplied during navigation.