父级的状态参数在$ urlRouterProvider.otherwise状态重新加载时为空

时间:2017-02-13 07:56:03

标签: angularjs angular-ui-router

我的状态配置定义如下:

    $stateProvider
      .state('parentState', {
        abstract: true,
        url: '/:tenantId/',
        param: {
          tenantId: {
            array: false
          }
        },
        views: {
          'header@': {
            templateUrl: 'home/header.html',
            controller: 'Header as vm'
          },
          'footer@': {
            templateUrl: 'home/footer.html',
            controller: 'FooterCtrl as vm'
          }
        },
        resolve: userResolve,
        data: {
          private: true
        }
      })
      //...4-5 other child states, then a state to handle unknown urls
      .state('parentState.otherwise', {
        views: {
          '@': {
            templateUrl: 'home/404/pageNotFound.html',
            controller: 'PageNotFoundCtrl as vm'
          }
        }
      });
    $urlRouterProvider.otherwise(function ($injector) {
      $injector.get('$state').go('parentState.otherwise', {}, {
        location: false
      });
    });

现在,当输入无效网址时,parentState.otherwise状态正确加载,parentState参数(即tenantId)也正确填充。 但是,在具有相同无效URL的页面重新加载(刷新,Ctrl + R)上,parentState.otherwise状态加载,但问题是parentState参数,即tenantId将作为空字符串(“”)。

1 个答案:

答案 0 :(得分:0)

不知何故,对于location: false,父状态(在这种情况下为parentState)在页面刷新时没有从URL中获取tenantId参数。因此,如果我们明确地将tenantId param传递给子状态,即在打开它时parentState.otherwise,那么即使在页面刷新时,一切都能正常运行:

$urlRouterProvider.otherwise(function ($injector) {
  $injector.get('$state').go('parentState.otherwise', {
    tenantId: 'someTenantId'
  }, {
    location: false
  });
});