Mandatory urularJS ui router parameters

According to the documentation, urilejs ui-router url parameters are optional by default. So is there a way to create required parameters? For example, when the parameter is absent or null, it will not go to the page?

I hope you help me.

thank

+4
source share
2 answers

Use resolve UI routers to check if route parameters 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');
    }
  }
})
+5
source

, . , ui-router@0.4.2 , .

resolve: { 
    somekey: 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');
    }
}

, ngAnnotate

+2

All Articles