本文介绍了如何从ui路由器statechange返回$ state.current.name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我更改角度位置时,我想返回 .state('name')

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

从我的 run()可以返回 $ state 对象:

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

但是当我尝试获取 $ state.current 时,它是空对象

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

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

      console.log($state.current);

配置示例:

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


推荐答案

您可以收听'$ stateChangeSuccess'并设置状态。例如-

You can listen for '$stateChangeSuccess' and set state accrodingly. For example -

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

这篇关于如何从ui路由器statechange返回$ state.current.name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:27