31

seems like $stateParams is not working. passing date like this:

$state.go('state2', { someParam : 'broken magic' });

params being ignored on the target state

console.log('state2 params:', $stateParams); // return empty object {}

code:

    var app = angular.module('app', [
     'ui.router'
    ]);

    app.config(function($stateProvider) {
      $stateProvider
            .state('state1', {
                url: '',
                templateUrl: 'state-1.html',
                controller : function ($scope, $state, $stateParams) {
                  $scope.go = function () {
                    $state.go('state2', { someParam : 'broken magic' });
                  };

                  console.log('state1 params:', $stateParams);
                }
            })
            .state('state2', {
                url: 'state2',
                templateUrl: 'state-2.html',
                controller : function ($scope, $state, $stateParams) {
                  $scope.go = function () {
                    $state.go('state1', { someOtherParam : 'lazy lizard' });
                  };

                  console.log('state2 params:', $stateParams);
                }
            });
    });

Live example can be found here

thank you.

3
  • The example doesn't work for me. Commented Mar 24, 2014 at 13:40
  • How did you define your states? (Show us the $stateProvider part). Commented Mar 24, 2014 at 13:41
  • @kba, I updated my question to make it more clear. Commented Mar 24, 2014 at 13:47

5 Answers 5

63

You can't pass arbitrary parameters between states, you need to have them defined as part of your $stateProvider definition. E.g.

$stateProvider
    .state('contacts.detail', {
        url: "/contacts/:contactId",
        templateUrl: 'contacts.detail.html',
        controller: function ($stateParams) {
            console.log($stateParams);
        }
    }) ...

The above will output an object with the contactId property defined. If you go to /contacts/42, your $stateParams will be {contactId: 42}.

See the documentation for UI-Router URL Routing for more information.

Sign up to request clarification or add additional context in comments.

5 Comments

@nilskp Yes, that sucks, but you should rarely need it anyway; all your data should belong in your services/factories/providers. In AngularJS 2.0, I think they'll make arbitrary parameter passing possible.
I don't know. I prefer parameter passing over global state.
I just encountered this issue too and it seems to be an arbitrary restriction in ui-router: bit.ly/1qbkhRq
@nilskp I'm not saying you shouldn't use parameter passing. I'm saying when you do, it should be registered in the route. Going to a specific page with an ID? Add it to the url. Going to show an error message? Don't put it in the url—you don't want people linking to it anyway.
I'd like to pass actual state, not just a text string. Kinda hard to do in a URL.
54
+100

if you don't want to define your parameter in the url, you must include a params property on the state you are transitioning to. Otherwise the data will be removed from the $stateParams object. The format of the params object is an array of strings in older versions of angular-ui-router; in newer versions it is an object of empty objects:

params: { id: {}, blue: {}}

See this example:

$stateProvider.state('state1', {
    url: '',
    params: {
        id: 0,
        blue: ''
    },
    templateUrl: 'state-1.html',
    controller: function($scope, $state, $stateParams) {
        $scope.go = function() {
            $state.go('state2', {
                id: 5,
                blue: '#0000FF'
            });
        };

        console.log('state params:', $stateParams);
    }
});

Related question: Parameters for states without URLs in ui-router for AngularJS

7 Comments

This was exactly what I needed--hidden parameters that are not displayed in the URL. I have been searching for a couple of days now, and have not been able to find this in the UI-Router docs. Thank you so much!
NOTE: (on v0.2.10) If the state is a child state of a parent that has a URL parameter, then that parameter needs to be included in the params array.
This syntax will not work for newer versions. You need to declare it like this now params: { someParam: {} }. Source : stackoverflow.com/questions/22814554/… The documentation still states it should be an array which is wrong and very confusing.
FYI The empty objects blue: {} you're passing are treated as the default values. So pass blue: null instead if you're doing something like if ($stateParams.blue) ... in your controller.
|
6

Just passing parameters to a state is not enough. You have to define the parameter explicitly by name in the url property of your state.

If you don't do this, ui-router won't know this state is expecting a parameter and the $stateParams object will not be populated like you want.

Here is an example of how you might modify your state to expect a parameter, inject $stateParams, and do something with said parameter:

$stateProvider.state('state1', {
        url: '',
        templateUrl: 'state-1.html',
        controller : function ($scope, $state, $stateParams) {
          $scope.params = $stateParams;
          $scope.go = function () {
            $state.go('state2', { id : 'broken magic' });
          };

          console.log('state1 params:', $stateParams);
        }
    })

    .state('state2', {
            url: 'state2/:id',
            templateUrl: 'state-2.html',
            controller : function ($scope, $state, $stateParams) {
              $scope.params = $stateParams;
              $scope.go = function () {
                $state.go('state1', { someOtherParam : 'lazy lizard' });
              };

              console.log('state2 params:', $stateParams);
            }
        })

Here is a working example of passing state params on jsfiddle.

Comments

2

the solutions above works but for my case I needed to pass query parameter so I dit it like this:

$stateProvider
        .state('state1', {
            url: '/state1?other',
            templateUrl: 'state-1.html',
            controller : function ($scope, $state, $stateParams) {
              $scope.params = $stateParams;
              $scope.go = function () {
                $state.go('state2', { someParam : 'broken magic' });
              };

              console.log('state1 params:', $stateParams);
            }
        })
        .state('state2', {
            url: '/state2?someParam',
            templateUrl: 'state-2.html',
            controller : function ($scope, $state, $stateParams) {
              $scope.params = $stateParams;
              $scope.go = function () {
                $state.go('state1', { other : 'lazy lizard' });
              };

              console.log('state2 params:', $stateParams);
            }
        });

Comments

1

Make a transport and use it!

angular_app.factory('$$transport', function($q) {
    var transport;
    return transport = {
        dfr: $q.defer(),
        push: function(v) {
            return transport.dfr.resolve(v);
        },
        then: function(s, f) {
            if (f == null) {
                f = function() {};
            }
            return transport.dfr.promise.then(function(_s) {
                s(_s);
                transport.dfr = $q.defer();
                return transport.then(s, f);
            }, function(_f) {
                f(_f);
                transport.dfr = $q.defer();
                return transport.then(s, f);
            });
        }
    };
});
$stateProvider.state('state1', {
            url: '/state1?other',
            templateUrl: 'state-1.html',
            controller : function ($scope, $state, $$transport) {
              $$transport.then(function(s) {
                $scope.param = s
                console.log('state1 params:', s);
              });
              $scope.go = function () {
                $state.go('state2', { someParam : 'broken magic' });
              }
            }
        })
        .state('state2', {
            url: '/state2?someParam',
            templateUrl: 'state-2.html',
            controller : function ($scope, $state, $$transport) {
              $scope.go = function () {
                $$transport.push({other:'lazy lizard'});
                $state.go('state1');
              };
            }
        });

2 Comments

What is a transport and what is $$transport? Please provide some documentation. Thank you.
$$ should not be used. There are better solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.