使用stateProvider angularjs的多个可选参数的Url

Sam*_*ami 3 state angularjs angular-ui angular-ui-router

由于angular angularjs路由有可选的参数值吗?AngularJS:使用带有可选参数的URL进行路由带有?参数名称的 问号应该使其成为可选项.它没有帮助我.

var app = angular.module('app', ['ui.router','ngRoute']);

app.config(function ($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/a');
    $stateProvider.state('a', {
        url: '/a',
        templateUrl: 'views/a.html'
    }).state('b', {
        url: '/b/:code?/:d?',
        templateUrl : 'views/b.html'
    })
});
Run Code Online (Sandbox Code Playgroud)

这个网址http:// localhost:xx/kk /#/ b/1/2对我来说很好.但是http:// localhost:xx/kk /#/ b(没有任何参数)和http:// localhost:xx/kk /#/ b/1对我不起作用......

你可以看到我正在使用$stateProviderui-router.我不想切换到$urlRouteProvider

Rad*_*ler 5

一个工作的plunker

我们需要的是一个调用的设置params : {},它可以处理可选的参数:

 params: {
    code: {squash: true, value: null},
 }
Run Code Online (Sandbox Code Playgroud)

因此,国家定义将是

.state('b', {
    url: '/b/:code/:d',
    templateUrl : 'views/b.html',
    params: {
      code: {squash: true, value: null},
      d   : {squash: true, value: null},
    }
})
Run Code Online (Sandbox Code Playgroud)

这些将起作用:

<a href="#/b">
<a href="#/b/code1">
<a href="#/b/code22">
<a href="#/b/code333/d4">
Run Code Online (Sandbox Code Playgroud)

查看这些以获取更多详细信息和示例

还有一些神奇之处: