NewQueries
NewQueries

Reputation: 4931

$urlRouterProvider $stateProvider otherwise does not work

Hi below is my code for urlRouterProvider otherwise.

.config(function config($urlRouterProvider, $stateProvider) {
$urlRouterProvider.otherwise('/invalid');
$stateProvider.state('view', {
  url: '/view?{businessId:^[0-9]{1,20}$}',
  templateUrl: 'view/view.tpl.html',
  controller: 'ViewCtrl',
  controllerAs: 'vm' 
})
.state('otherwise', {
    url: '/invalid',
    templateUrl: 'view/view.error.tpl.html'
});
})

This is what i am expecting: 1. if i go to view?businessId=12345 it should load the page without any issues 2. if i go to view?businessId=abcd it should re-direct it to the view.error.tpl.html

But instead, it does nothing if i enter the invalid url. I reffered to this post for help but none of the options really work Otherwise on StateProvider

Upvotes: 0

Views: 523

Answers (2)

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3118

Try this

.config(function ($urlRouterProvider, $stateProvider) {
    $stateProvider.state('view', {
      url: '/view?{businessId:^[0-9]{1,20}$}',
      templateUrl: 'view/view.tpl.html',
      controller: 'ViewCtrl',
      controllerAs: 'vm' 
    })
    .state('otherwise', {
        url: '*path',
        templateUrl: 'view/view.error.tpl.html'
    });

})

Upvotes: 0

MayK
MayK

Reputation: 1329

you can try this, without using the otherwise function:

$stateProvider.state('view', {
  url: '/view?{businessId:^[0-9]{1,20}$}',
  templateUrl: 'view/view.tpl.html',
  controller: 'ViewCtrl',
  controllerAs: 'vm' 
})
.state('otherwise', {
    url: '*path',
    templateUrl: 'view/view.error.tpl.html'
});

Upvotes: 2

Related Questions