$ urlRouterProvider $ stateProvider否则无效

时间:2016-05-20 18:32:09

标签: angularjs angular-ui-router angular-ui

下面是我的urlRouterProvider代码。

.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'
});
})

这是我所期待的: 1.如果我去查看?businessId = 12345它应该加载页面没有任何问题 2.如果我去查看?businessId = abcd它应该将它重定向到view.error.tpl.html

但是,如果我输入无效的网址,它什么都不做。 我在这篇文章中提到了帮助,但没有一个选项真正起作用Otherwise on StateProvider

2 个答案:

答案 0 :(得分:2)

你可以尝试这个,而不使用其他功能:

$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'
});

答案 1 :(得分:0)

试试这个

.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'
    });

})