Efficient Data Passing Between States in AngularJS UI-Router Using Non-URL Parameters

Dec 07, 2025 · Programming · 9 views · 7.8

Keywords: AngularJS | UI-Router | Non-URL Parameters | Data Passing

Abstract: This technical article explains how to securely pass data between states in AngularJS UI-Router without exposing it in the URL. It details the use of the params object, provides practical code examples, and discusses implementation strategies for non-URL parameters.

In AngularJS applications using UI-Router, a common requirement is to pass data between different states without exposing sensitive or lengthy information in the URL. This is crucial for maintaining security and user experience, especially when dealing with server responses or confidential data.

Understanding Non-URL Parameters in UI-Router

UI-Router provides a params property in state definitions to handle both URL and non-URL parameters. As highlighted in the documentation, the params object can define additional non-url parameters, allowing data to be passed between states without being part of the URL.

For example, consider a scenario with two states, 'A' and 'B'. In state 'A', a server call is made, and the response, which is a long string message, needs to be passed to state 'B'. Using URL parameters would expose this data, so non-URL parameters are the ideal solution.

Implementing Non-URL Parameters

To implement this, define the states with the params object. Here is a refactored example based on the core concepts:

// State definition with non-URL parameters
$stateProvider
  .state('stateA', {
    url: "/state-a",
    templateUrl: 'stateA.html',
    params: {
      serverResponse: null // Non-URL parameter to hold the server response
    }
  })
  .state('stateB', {
    url: "/state-b",
    templateUrl: 'stateB.html',
    params: {
      receivedData: null // Parameter to receive data from state A
    }
  });

In this example, serverResponse and receivedData are non-URL parameters. To pass data from state A to state B, use the $state.go() method or ui-sref directive.

// Passing data from a controller
$state.go('stateB', {
  receivedData: serverResponse // serverResponse is the data from state A
});

// Or in HTML using ui-sref
<a ui-sref="stateB({ receivedData: 'someLongString' })">Go to State B</a>

The params object can also include default values and other configurations, such as array parameters or squash options for handling defaults in the URL.

// Example with default values and configurations
params: {
  param1: { value: "defaultValue" },
  param2: "defaultValue2",
  param3: { array: true }
}

Accessing Parameters in Controllers

Once the data is passed, it can be accessed in the controller using the $stateParams service. This is a key feature that allows the parameters to be available without being part of the URL.

.controller('stateBCtrl', function($stateParams, $scope) {
  $scope.data = $stateParams.receivedData; // Access the non-URL parameter
});

This ensures that the data remains secure and is not exposed in the browser's address bar, making it suitable for sensitive information.

Conclusion

Using non-URL parameters in AngularJS UI-Router is an effective way to pass data between states securely. By leveraging the params object, developers can maintain clean URLs while handling complex data transfers. This approach enhances application security and user privacy, especially in scenarios involving long strings or confidential data.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.