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

Spread the love

Question Description

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

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

 .run(function($rootScope, Analytics, $location, $stateParams, $state) {
      console.log($state);

working object

but when I try to get$state.current it is empty object

.run(function($rootScope, $location, $stateParams, $state) {

      console.log($state.current);

empty

config example:

 .config(function($stateProvider, $urlRouterProvider, AnalyticsProvider) {  
        $urlRouterProvider.otherwise('/');
        $stateProvider
        .state('home', {
            url: '/',
            views: {
              '': {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl'
              },
              '[email protected]': {
                templateUrl: 'views/partials/navigation.html',
                controller: 'NavigationCtrl'
              },
              '[email protected]': {
                templateUrl: 'views/partials/weekly.html',
                controller: 'WeeklyCtrl'
              },
              '[email protected]': {
                templateUrl: 'views/partials/side-panel.html',
                controller: 'SidePanelCtrl'
              },
              '[email protected]': {
                templateUrl: 'views/partials/shop-panel.html',
                controller: 'ShopPanelCtrl'
              },
              '[email protected]': {
                templateUrl: 'views/partials/footer.html',
                controller: 'FooterCtrl'
              }
            }
          })

Practice As Follows

You can listen for ‘$stateChangeSuccess’ and set state accrodingly. For example –

$rootScope.$on('$stateChangeSuccess',
  function(event, toState, toParams, fromState, fromParams) {
    $state.current = toState;
  }
)

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.