Jed Jed - 1 year ago 230
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.


Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download