AngularJS ui router passing data between states without URL

vijay tyagi

I am facing this problem of passing data between two states without exposing the data in the url, it's like user cannot really directly land on this state.

For example. I have two states "A" and "B". I am doing some server call in state "A" and passing the response of the call to state "B". The response of the server call is a string message, which is quite long, so i cannot expose that in the url.

So is there any way in angular ui router to pass data between states, without using url params ?

Radim Köhler

We can use params, new feature of the UI-Router:

API Reference / ui.router.state / $stateProvider

params A map which optionally configures parameters declared in the url, or defines additional non-url parameters. For each parameter being configured, add a configuration object keyed to the name of the parameter.

See the part: "...or defines additional non-url parameters..."

So the state def would be:

$stateProvider
  .state('home', {
    url: "/home",
    templateUrl: 'tpl.html',
    params: { hiddenOne: null, }
  })

Few examples form the doc mentioned above:

// define a parameter's default value
params: {
  param1: { value: "defaultValue" }
}
// shorthand default values
params: {
  param1: "defaultValue",
  param2: "param2Default"
}

// param will be array []
params: {
  param1: { array: true }
}

// handling the default value in url:
params: {
  param1: {
    value: "defaultId",
    squash: true
} }
// squash "defaultValue" to "~"
params: {
  param1: {
    value: "defaultValue",
    squash: "~"
  } }

EXTEND - working example: http://plnkr.co/edit/inFhDmP42AQyeUBmyIVl?p=info

Here is an example of a state definition:

 $stateProvider
  .state('home', {
      url: "/home",
      params : { veryLongParamHome: null, },
      ...
  })
  .state('parent', {
      url: "/parent",
      params : { veryLongParamParent: null, },
      ...
  })
  .state('parent.child', { 
      url: "/child",
      params : { veryLongParamChild: null, },
      ...
  })

This could be a call using ui-sref:

<a ui-sref="home({veryLongParamHome:'Home--f8d218ae-d998-4aa4-94ee-f27144a21238'
  })">home</a>

<a ui-sref="parent({ 
    veryLongParamParent:'Parent--2852f22c-dc85-41af-9064-d365bc4fc822'
  })">parent</a>

<a ui-sref="parent.child({
    veryLongParamParent:'Parent--0b2a585f-fcef-4462-b656-544e4575fca5',  
    veryLongParamChild:'Child--f8d218ae-d998-4aa4-94ee-f27144a61238'
  })">parent.child</a>

Check the example here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS ui-router default state for states without a URL

From Dev

Angular UI-Router passing data between states with go function does not work

From Java

How do I share $scope data between states in angularjs ui-router?

From Dev

Parameters for states without URLs in ui-router for AngularJS

From Dev

AngularJS ui-Router - hide $stateParams when navigating between states

From Dev

[AngularJS][ui-router]: optional params in states in the middle of URL

From Dev

Sharing ui-router 'custom data' between sibling states

From Dev

How to pass data between angular ui-router states?

From Dev

Sharing ng-model data between states in ui-router

From Dev

Passing data in between controller without help of url?

From Dev

Passing data in between controller without help of url?

From Dev

How to get Angularjs UI Router list of states?

From Dev

angularjs - ui.router sibling states

From Dev

AngularJS, ui-router, nested states

From Dev

AngularJS UI Router shared nested routes/states

From Dev

angularjs - ui.router sibling states

From Dev

Angularjs ui.router chid states not loading

From Dev

Parsing UI router states to URLs without Angular

From Dev

Angular ui router - Share views between states

From Dev

Sending events between ui-router states

From Dev

Sending events between ui-router states

From Dev

Angular UI Router: Different states with same URL?

From Dev

URL Routing for nested states in ui-router

From Dev

Angular ui-router passing data between subviews of the same state

From Dev

AngularJS ui-router: URL with params and without params

From Dev

AngularJS : Making UI-Router work without a URL

From Java

AngularJS UI Router - change url without reloading state

From Dev

AngularJS : Making UI-Router work without a URL

From Dev

Retaining/passing parameters across deep states with angular and ui-router

Related Related

  1. 1

    AngularJS ui-router default state for states without a URL

  2. 2

    Angular UI-Router passing data between states with go function does not work

  3. 3

    How do I share $scope data between states in angularjs ui-router?

  4. 4

    Parameters for states without URLs in ui-router for AngularJS

  5. 5

    AngularJS ui-Router - hide $stateParams when navigating between states

  6. 6

    [AngularJS][ui-router]: optional params in states in the middle of URL

  7. 7

    Sharing ui-router 'custom data' between sibling states

  8. 8

    How to pass data between angular ui-router states?

  9. 9

    Sharing ng-model data between states in ui-router

  10. 10

    Passing data in between controller without help of url?

  11. 11

    Passing data in between controller without help of url?

  12. 12

    How to get Angularjs UI Router list of states?

  13. 13

    angularjs - ui.router sibling states

  14. 14

    AngularJS, ui-router, nested states

  15. 15

    AngularJS UI Router shared nested routes/states

  16. 16

    angularjs - ui.router sibling states

  17. 17

    Angularjs ui.router chid states not loading

  18. 18

    Parsing UI router states to URLs without Angular

  19. 19

    Angular ui router - Share views between states

  20. 20

    Sending events between ui-router states

  21. 21

    Sending events between ui-router states

  22. 22

    Angular UI Router: Different states with same URL?

  23. 23

    URL Routing for nested states in ui-router

  24. 24

    Angular ui-router passing data between subviews of the same state

  25. 25

    AngularJS ui-router: URL with params and without params

  26. 26

    AngularJS : Making UI-Router work without a URL

  27. 27

    AngularJS UI Router - change url without reloading state

  28. 28

    AngularJS : Making UI-Router work without a URL

  29. 29

    Retaining/passing parameters across deep states with angular and ui-router

HotTag

Archive