4

I have a weird issue I don't understand:

I'm using ui-router. Why do I get this results when I console.log in app.run():

app.run(function ($state) { console.log($state); // output: Object{params:{sitename:"mysite"},current:{...},... other properties} console.log($state.params); // output: Object {} 

First I print out $state and it has a "params" property which is an object with a property called "sitename". Then I print out the $state.params property and it's suddenly empty.

Can anyone explain this?

BTW. Also tried this but same result

console.log($state["params"]); // output: Object {} 
2
  • did you tried $stateParams service? Commented Jun 11, 2015 at 9:34
  • 2
    yes i did. $stateParams is also empty. but it does not answer my question! Commented Jun 11, 2015 at 9:42

2 Answers 2

1

I encountered the same issue.

Basically, console.log($state) is printing a reference to the $state dependency, but NOT the actually value of $state. (Read more here)

Therefore when you try to access $state.params, it returns undefined. $state will actually have params if ui-router finishes routing (after $stateChangeSuccess). In this situation we attached custom data to the state object. (read more about routing and attachment)

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

Comments

0

I met with the same problem. I suppose could be due to two things: 1. $ stateParams not exist at the time the call 2. $ stateParams not tied to the caller.

Anyway, I solved this problem as follows:

.run(['$rootScope', function ($rootScope) { $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { $rootScope.toParams = toParams; }); }]) 

and use in my object $rootScope.toParams:

 params: { page: $rootScope.toParams.page, } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.