how do return the $state.current.name from ui-router statechange

前端 未结 4 1494
野性不改
野性不改 2020-12-28 13:49

I would like to return the .state(\'name\') when I change location in angular.

From my run() it can return the $state object:

4条回答
  •  情歌与酒
    2020-12-28 14:44

    As an alternative to Sandeep's solution, you can also do it this way:

    angular.module('yourApp').run(['$rootScope', '$state',
        function ($rootScope, $state) {
            $rootScope.$state = $state;
        }
    ]);
    

    Doing it this way, it only gets set once instead of on every stateChange. Basically you just store a reference to $state somewhere - I chose $rootScope just to keep this example simple.

    Now in my code I can easily reference it like:

    and

    Current State: {{$state.current.name}}

    I also generally store $stateParams as well so I have even more information about the state (but I left it out of this example).

提交回复
热议问题