AngularJS可选的路由参数在开头

时间:2021-10-29 08:04:02

I'm trying to configure my AngularJS app with optional route parameter.

我正在尝试使用可选的路由参数配置我的AngularJS应用程序。

The URLs that I need to support may have a locale at the beginning. e.g.

我需要支持的URL可能在开头有一个区域设置。例如

  • /fr-FR/Welcome
  • /Welcome

I tried the following

我尝试了以下内容

$routeProvider.when('/:locale?/Welcome', {
...
})

However, it seems, it satisfies the "/fr-FR/Welcome" case and not the "/Welcome" case.

但是,似乎它满足了“/ fr-FR / Welcome”案例,而不是“/ Welcome”案例。

Is it because I'm always prepending a "/" in the beginning.

是因为我在开头时总是先加上“/”。

Will the following work?

以下工作会吗?

$routeProvider.when('/:locale/?Welcome', {
...
})

2 个解决方案

#1


https://docs.angularjs.org/api/ngRoute/service/$route#example

.config(function($routeProvider, $locationProvider) {
      $routeProvider
       .when('/Book/:bookId', {
        templateUrl: 'book.html',
        controller: 'BookController',
        resolve: { 
        }
      })
      .when('/Book/:bookId/ch/:chapterId', {
        templateUrl: 'chapter.html',
        controller: 'ChapterController'
      });

Also you can use for multiple language support like this tutorials

您也可以使用本教程中的多语言支持

https://scotch.io/tutorials/internationalization-of-angularjs-applications

#2


It is not possible in Angular. remember /:locale is not optional. it is route parameter which means its value could be different but it should be there to execute that route (controller and template).

在Angular中是不可能的。记住/:locale不是可选的。它是路由参数,这意味着它的值可能不同,但它应该在那里执行该路由(控制器和模板)。

like

/fr/Welcome

/en/Welcome

fr and en must be there which help angular to select that route.

fr和en必须在那里帮助角度来选择那条路线。

#1


https://docs.angularjs.org/api/ngRoute/service/$route#example

.config(function($routeProvider, $locationProvider) {
      $routeProvider
       .when('/Book/:bookId', {
        templateUrl: 'book.html',
        controller: 'BookController',
        resolve: { 
        }
      })
      .when('/Book/:bookId/ch/:chapterId', {
        templateUrl: 'chapter.html',
        controller: 'ChapterController'
      });

Also you can use for multiple language support like this tutorials

您也可以使用本教程中的多语言支持

https://scotch.io/tutorials/internationalization-of-angularjs-applications

#2


It is not possible in Angular. remember /:locale is not optional. it is route parameter which means its value could be different but it should be there to execute that route (controller and template).

在Angular中是不可能的。记住/:locale不是可选的。它是路由参数,这意味着它的值可能不同,但它应该在那里执行该路由(控制器和模板)。

like

/fr/Welcome

/en/Welcome

fr and en must be there which help angular to select that route.

fr和en必须在那里帮助角度来选择那条路线。