问题描述:

I am using the following url in my state:

 .state('forum.spesific', {

url: '/:articleId',

templateUrl: 'modules/forum/client/views/forum.client.view.html',

controller: 'forumController',

controllerAs: 'vm',

})

.state('forum.spesific2', {

url: '/:articleId',

templateUrl: 'modules/forum/client/views/forum2.client.view.html',

controller: 'forumController',

controllerAs: 'vm',

})

how can i make one of them rely on that the :articleId is only number, and the other mix of numbers and characters?

like:

 .state('forum.spesific', {

url: '/:articleId ONLY BY NUMBER',

templateUrl: 'modules/forum/client/views/forum.client.view.html',

controller: 'forumController',

controllerAs: 'vm',

})

.state('forum.spesific', {

url: '/:articleId BOTH ( have to be both)',

templateUrl: 'modules/forum/client/views/forum2.client.view.html',

controller: 'forumController',

controllerAs: 'vm',

})

网友答案:

you can use the regex-like syntax: '/articles/{id:int}', that matches only integers. By the way, this is not very-safe because id (eg: bigInt) can be out of the javascript safe integer range... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger

网友答案:

Maybe you can merge those 2 states into the same and change the templateUrl

templateUrl: function (stateParams){ if(isNaN(stateParams.articleId)){ return "modules/forum/client/views/forum2.client.view.html" } else { return "modules/forum/client/views/forum.client.view.html"; } }

Hope this helps

网友答案:

you can specify regex like so:

 .state('forum.spesific', {
      url: '/{articleId:[0-9]+}',
      templateUrl: 'modules/forum/client/views/forum.client.view.html',
      controller: 'forumController',
      controllerAs: 'vm',
    })
.state('forum.spesific2', {
      url: '/{articleId:[a-zA-Z0-9]+}',
      templateUrl: 'modules/forum/client/views/forum2.client.view.html',
      controller: 'forumController',
      controllerAs: 'vm',
    })

but why 2 states if the only difference is the id type ?

相关阅读:
Top