$ state params is empty

I have a strange problem that I do not understand:

I am using ui-router. Why do I get these 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 $ state and has a params property, which is an object called "sitename". Then I print the $ state.params property and it is unexpectedly empty.

Can anyone explain this?

BTW. Also tried this one but the same result

console.log($state["params"]); // output: Object {}
+4
source share
1 answer

I ran into the same problem. I suppose this could be due to two things: 1. $ stateParams does not exist at the time of the call 2. $ stateParams are 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 $ rootScope.toParams in my object:

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

All Articles