Jed Jed - 5 months ago 106
AngularJS Question

AngularJS ui router mandatory parameters

Based on the documentation, angularjs ui-router url parameters are by default optional. So is there a way to create mandatory parameters? Like when the parameter is missing or null it will not proceed to the page?

Hope you can help me.

Thanks

Answer

Use UI Routers resolve to check if route params are missing.

//Example of a single route
.state('dashboard', {
  url: '/dashboard/:userId',
  templateUrl: 'dashboard.html',
  controller: 'DashboardController',
  resolve: function($stateParams, $location){

    //Check if url parameter is missing.
    if ($stateParams.userId === undefined) {
      //Do something such as navigating to a different page.
      $location.path('/somewhere/else');
    }
  }
})