Angular UI路由器;如何不匹配空参数

时间:2017-12-11 11:10:37

标签: angularjs angular-ui-router

我的网站有两条主要路线:

/home
/:something

对/ home的任何请求都应该转到/ home并且这是通过使用'否则'完成的,其他任何与home不匹配且不为空的其他内容应该转到app.lista。问题是,app.lista匹配/因为就像:slug是空的,所以否则不被调用:

$stateProvider
    .state('app', {
      url: '/',
      abstract: true,
      templateUrl: '/_/common/templates/main.html',
      controller: 'main'
    })
    .state('app.home', {
      url: 'home',
      views: {
        sectionHolder: {
          templateUrl: '/_/home/templates/home.html',
          controller: 'home'
        }
      }
    })
    .state('app.lista', {
      url: ':slug',
      views: {
        sectionHolder: {
          templateUrl: '/_/home/templates/list.html',
          controller: 'list'
        }
      }
    });

$urlRouterProvider.otherwise('/home');

如果:slug为空,我如何告诉stateProvider与app.lista不匹配?

3 个答案:

答案 0 :(得分:0)

看起来在github上记录Here的同一问题,解决方案似乎是使用ragular表达式

我认为将您的slug route更改为

.state('app.lista', {
      url: '/{slag:[^\/]+}',
      views: {
        sectionHolder: {
          templateUrl: '/_/home/templates/list.html',
          controller: 'list'
        }
      }
    });

应解决您的问题plunker

答案 1 :(得分:0)

使您的应用路由非抽象,并将模板和控制器更改为指向app.home的模板和控制器。由于它是在app.list之前定义的,因此'/'将匹配app路由。

$stateProvider
    .state('app', {
      url: '/',
      templateUrl: '/_/common/templates/home.html',
      controller: 'home'
    })
    .state('app.home', {
      url: 'home',
      views: {
        sectionHolder: {
          templateUrl: '/_/home/templates/home.html',
          controller: 'home'
        }
      }
    })
    .state('app.lista', {
      url: ':slug',
      views: {
        sectionHolder: {
          templateUrl: '/_/home/templates/list.html',
          controller: 'list'
        }
      }
    });

$urlRouterProvider.otherwise('/home');

答案 2 :(得分:0)

使用UI路由器resolve检查路由参数是否缺失。

   #2. initialise variables 
   def __init__(self):
        self.text = "hello world"

  #3. instantiate the class

 from <name of python class without py> import *

 classInstance = testHello()

 #4. print desired variable 
 print(classInstance.text)