【问题标题】:default child inside father in $stateprovider with typescript$stateprovider 中的父亲内的默认孩子与打字稿
【发布时间】:2016-02-19 05:11:31
【问题描述】:

我有一个 html 页面父亲:

<div id="..." class="container">
   <pp-nav></pp-nav>
   <div ui-view></div>
</div>

这是我的控制器,有一个父亲和两个孩子:

export function routerConfig($stateProvider: angular.ui.IStateProvider, $urlRouterProvider: angular.ui.IUrlRouterProvider) {
$stateProvider
.state('gestionePeP', {
  url: '/gestionePeP',
  templateUrl: '.../gestionePeP/gestionePeP.html'
})
.state('gestionePeP.puntiAttivazione', {
  url: '/puntiAttivazione',
  templateUrl: '.../gestionePeP/puntiAttivazione.html'
})
.state('gestionePeP.gestioneContenuti', {
  url: '/gestioneContenuti',
  templateUrl: '.../gestionePeP/gestioneContenuti.html'
});

我希望当我转到父页面时,stateprovider 打开父 html 页面,其中包含第一个子页面。 我该怎么做?

如果我用

将链接放在父亲的按钮内
ui-sref="gestionePeP.puntiAttivazione" 

它会打开正确的页面。但我想要没有任何点击的相同行为。 我试过 $state.go('gestionePeP.puntiAttivazione') 但它进入了循环。 我该如何解决?

【问题讨论】:

    标签: typescript angular-ui-router angular-ui state


    【解决方案1】:

    我发现最好的方法还是在这里:

    Redirect a state to default substate with UI-Router in AngularJS

    只需将这些行添加到一些 .run()

    app.run(['$rootScope', '$state', function($rootScope, $state) {
    
        $rootScope.$on('$stateChangeStart', function(evt, to, params) {
          if (to.redirectTo) {
            evt.preventDefault();
            $state.go(to.redirectTo, params)
          }
        });
    }]);
    

    并使用默认redirectTo扩展状态def:

    .state('gestionePeP', {
      url: '/gestionePeP',
      templateUrl: '.../gestionePeP/gestionePeP.html',
      // this will set redirect target
      redirectTo: 'gestionePeP.puntiAttivazione',
    })
    

    检查that link 以确保工作人员

    EXTEND - 如何让 ng.ui.IState 知道新属性 redirectTo?

    只需将这些行添加到您的代码中:

    declare module angular.ui { export interface IState { redirectTo?: string; } }
    

    【讨论】:

    • redirectTo 在 IState 类型中不存在。
    • 成为IState 的一部分并不难,对吧? ;) 扩展答案应该显示如何 ;)
    • 不知道在哪里添加app.run函数
    • 你观察过我的小家伙吗?
    • 是的,但我没有 app.js 文件
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 2012-05-04
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2017-12-02
    • 2021-11-18
    • 2023-04-06
    相关资源
    最近更新 更多