default child inside father in $stateprovider with typescript

◇◆丶佛笑我妖孽 提交于 2019-12-24 12:23:08

问题


I have an html page father with:

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

This is my controller with a father and two child:

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'
});

I want that when I go to the father page the stateprovider opens the father html page with the first child inside it. How can I do it?

If I put a link inside a button in the father with

ui-sref="gestionePeP.puntiAttivazione" 

it opens the right page. But I want the same behaviour without any click. I tried $state.go('gestionePeP.puntiAttivazione') but it goes in loop. How can I solve?


回答1:


Still far the best way I found is here:

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

just add these lines to some .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)
      }
    });
}]);

And extend state def with default redirectTo:

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

Check that link to se working plunker

EXTEND - how to make ng.ui.IState aware of the new property redirectTo?

Just add these lines to your code:

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


来源:https://stackoverflow.com/questions/33763585/default-child-inside-father-in-stateprovider-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!