2

We are on a project which has the following multi level navigation tree:

main {
  inactive
  profile
  active
  admin {
    settings
    registration
    activePupil
    activeAdmin
    emergencyDetail {
      photos
    }
  }
}

And the actual Code:

$stateProvider
  // this state is placed in the <ion-nav-view> in the index.html
    .state('main', {
      url: '/main',
      abstract: true,
      templateUrl: 'main/templates/menu.html',
      controller: 'MenuCtrl as menu'
    })
    .state('main.inactive', {
      url: '/inactive',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/inactive.html',
          controller: 'InactiveCtrl as inactive'
        }
      }
    })
    .state('main.profile', {
      url: '/profile',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/profile.html',
          controller: 'ProfileCtrl as profile'
        }
      }
    })
    .state('main.active', {
      url: '/active',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/active.html',
          controller: 'ActiveCtrl as active'
        }
      }
    })
    .state('main.admin', {
      url: '/admin',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/admin.html',
          controller: 'AdminCtrl as admin'
        }
      }
    })
    .state('main.adminSettings', {
      url: '/admin/settings',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/admin-settings.html',
          controller: 'AdminSettingsCtrl as adminsettings'
        }
      }
    })
    .state('main.adminRegistration', {
      url: '/admin/registration',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/admin-registration.html',
          controller: 'AdminRegistrationCtrl as adminregistration'
        }
      }
    })
    .state('main.adminActivePupils', {
      url: '/admin/active/pupils',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/pupils-active.html',
          controller: 'PupilsActiveCtrl as pupilsactive'
        }
      }
    })
    .state('main.adminActiveAdmins', {
      url: '/admin/active/admins',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/admin-active.html',
          controller: 'AdminActiveCtrl as adminactive'
        }
      }
    })
    .state('main.adminEmergencyDetail', {
      url: '/admin/emergency/:id',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/admin-emergency-detail.html',
          controller: 'AdminEmergencyCtrl as adminemergency'
        }
      }
    })
    .state('main.adminEmergencyDetailPhotos', {
      url: '/admin/emergency/:id/photos',
      views: {
        'pageContent': {
          templateUrl: 'main/templates/admin-emergency-photos.html',
          controller: 'AdminEmergencyCtrl as adminemergency'
        }
      }
    });
  $urlRouterProvider.otherwise('/main/inactive');

Unfortunately the currently existing answers aren't helping us at all. The same goes for the wiki of Angular UI-Router. How should we change the states that something like...

$state.go('main.admin.emergencyDetail.photos')

...is possible and so going back to main.admin.emergencyDetail will be possible?

1 Answer 1

1

Seems (If I do understand the issue) that you can just change the current flat state structure into hierarchical one. That's it.

Usually, child should inject its content into parent's view, but we can even keep the logic, that all children are targeting grand parent (main) view:

// admin will stay
.state('main.admin', {
  url: '/admin',
  views: {
    'pageContent': { ... }
  }
})    
// state nesting starts here
.state('main.adminEmergencyDetail', {
  // no need the use parent url .. it will be inherited
  //url: '/admin/emergency/:id',
  url: '/emergency/:id',
  views: {
    // target is main view
    //'pageContent': { ... }
    'pageContent@main': { ... }
  }
})
// same for all other siblings of the EmergencyDetail
...
// and grand child
.state('main.admin.EmergencyDetail.Photos', {
  // grand parent and parent url will be already in play
  //url: '/admin/emergency/:id/photos',
  url: '/photos',
  views: {
    // target is main view
    //'pageContent': { ... }
    'pageContent@main': { ... }
  }
});

And now we can go to the states as required

$state.go('main.admin.emergencyDetail.photos', {id: 12345});

NOTE: all children are injecting its view into main target 'pageContent'. I would suggest to add a targets into each state (i.e. into admin and emergencyDetail) so, children are not replacing parent

Some example with working plunker

Nested states or views for layout with leftbar in ui-router?

3
  • 1
    Perfect. This solved our problem. Thanks for the clear answer. May 15, 2016 at 9:42
  • Somehow this works only for webbrowser view. As soon i try this wrapped on a phone (for example ionic), then the same behaviour returns. Same problem occures when emulating a mobile device in a browser (chrome). It seems not to be a css or html (overlapping back buttons) problem. Is the behaviour of the state history different on a mobile device? May 15, 2016 at 16:15
  • UI-Router state behavior is independent on browser vs mobile. But maybe ionic is adding some own handling. In general, you should not be experiencing the difference. but check the view caching issue, maybe here stackoverflow.com/a/34417037/1679310 May 15, 2016 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.