AngularJS ui router passing data between states without URL

140,278

Solution 1

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

Solution 2

The params object is included in $stateParams, but won't be part of the url.

1) In the route configuration:

$stateProvider.state('edit_user', {
    url: '/users/:user_id/edit',
    templateUrl: 'views/editUser.html',
    controller: 'editUserCtrl',
    params: {
        paramOne: { objectProperty: "defaultValueOne" },  //default value
        paramTwo: "defaultValueTwo"
    }
});

2) In the controller:

.controller('editUserCtrl', function ($stateParams, $scope) {       
    $scope.paramOne = $stateParams.paramOne;
    $scope.paramTwo = $stateParams.paramTwo;
});

3A) Changing the State from a controller

$state.go("edit_user", {
    user_id: 1,                
    paramOne: { objectProperty: "test_not_default1" },
    paramTwo: "from controller"
});

3B) Changing the State in html

<div ui-sref="edit_user({ user_id: 3, paramOne: { objectProperty: 'from_html1' }, paramTwo: 'fromhtml2' })"></div>

Example Plunker

Share:
140,278

Related videos on Youtube

vijay tyagi
Author by

vijay tyagi

Updated on May 13, 2020

Comments

  • vijay tyagi
    vijay tyagi about 4 years

    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 ?

  • vijay tyagi
    vijay tyagi over 9 years
    As suggested, i have tried using params on a nested state(Example - "/user/profile/contacts", it gave me some error. Do i need to define the "params" for parent state as well ?
  • vijay tyagi
    vijay tyagi over 9 years
    It seems it is not necessary to have parent have the params defined, thanks for the detailed answer.
  • vijay tyagi
    vijay tyagi over 9 years
    Unfortunately this doesn't work with ui-router 0.2.10 and that is what i am using, got this error on changing the version to 0.2.10 - Error: Invalid params in state 'home'
  • Radim Köhler
    Radim Köhler over 9 years
    @vijaytyagi, you should upgrade. Simply, there is no reason to keep running old version. And as far as I remember, the move should be smooth...
  • Ilan Biala
    Ilan Biala about 9 years
    Should $state.go('parent', { veryLongParamParent: 123 }); also work?
  • gwin003
    gwin003 almost 9 years
    @RadimKöhler Can we pass params using $state.go like @IIanBiala suggested?
  • Radim Köhler
    Radim Köhler almost 9 years
    @gwin003 (In case I do understand your question correctly) - as discussed here Difference between ui-sref and $state.go in AngularJS UI-Router ui-sref and $state.go are in fact the same concept behind. So the answer is yes...
  • ygesher
    ygesher over 8 years
    You can pass arbitrary objects this way, as well, as of v0.2.13
  • Elisabeth
    Elisabeth over 8 years
    @Ryan.lay Agree! When you are on a customers\edit\1 page and do a page refresh the passed data is of course null. Thus this approach can NOT work for passing dynamic data from state1 to state2.
  • Radim Köhler
    Radim Köhler over 8 years
    @Elisabeth, passing not url params in SPA could work only inside of that "page" instance. URL params are solutions for any other scenario
  • Jay Shukla
    Jay Shukla over 7 years
    I'm using FB api and getting token from the FB as a part of URL. I want to hide that token from the url. I tried with params but it won't work as expected. Can you help?
  • Michael K
    Michael K almost 7 years
    Thanks, perfect. Also noticed that if you want both $state and $stateParams, you only need to inject $state. The stateParams object is a property of $state: $state.params.
  • Aritz
    Aritz almost 7 years
    It must be told that step 1 (providing default values) is required to make the rest of them work ;-)
  • Roboprog
    Roboprog over 6 years
    Thank you for the concise example. Works for me! (UI-Router v 1.0.3)