1

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?

Ciro
  • 253
  • 2
  • 5
  • 23

1 Answers1

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; } }
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335