I am using AngularJS v1.2.0-rc.2 with ui-router v0.2.0. I want to pass the referrer state to another state so I use the toParams
of $state.go
like so:
$state.go('toState', {referer: $state.current.name});
According to the docs, this should populate the $stateParams
on the toState
controller, but it is undefined
. What am I missing?
I've created a plunk to demonstrate:
Not sure if it will work with AngularJS v1.2.0-rc.2 with ui-router v0.2.0. I have tested this solution on AngularJS v1.3.14 with ui-router v0.2.13.
I just realize that is not necessary to pass the parameter in the URL as gwhn recommends.
Just add your parameters with a default value on your state definition. Your state can still have an Url value.
and add the param to
$state.go()
Your define following in router.js
Your controller need add $stateParams.
You can send an object by parameter as follows.
The Nathan Matthews's solution did not work for me but it is totally correct but there is little point to reaching a workaround:
The key point is: Type of defined parameters and toParamas of $state.go should be same array or object on both sides of state transition.
For example when you define a params in a state as follows you means params is array because of using "[]":
So also you should pass toParams as array like this:
And you can access them via $stateParams as array like this:
Better solution is using object instead of array in both sides:
I replaced [] with {} in params definition. For passing toParams to $state.go also you should using object instead of array:
then you can access them via $stateParams easily:
I've spent a good deal of time fighting with Ionic / Angular's $state & $stateParams;
To utilize $state.go() and $stateParams you must have certain things setup and other parameters must not be present.
In my app.config() I've included $stateProvider and defined within it several states:
The
params
key is especially important. As well, notice there are NOurl
keys present... utilizing stateParams and URLs do NOT mix. They are mutually exclusive to each other.In the $state.go() call, define it as such:
The
index
andanotherKey
$stateParams variables will ONLY be populated if they are first listed in the $stateControllerparams
defining key.Within the controller, include $stateParams as illustrated:
The passed variables should be available!
The solution we came to having a state that took 2 parameters was changing:
to
All I had to do was add a parameter to the url state definition like so
Doh!