选项​​URL路径的UI路由器路由器、路径、选项、URL

2023-09-13 03:47:44 作者:彼年豆蔻き

在我的角度应用,我大量使用嵌套的状态与UI的路由器。

In my angular application, I am making heavy use of nested states with UI-Router.

我想创建一个确定应用程序的基础上,有一个可选的语言环境路径URL中的语言环境的父状态。

I am trying to create a parent state that determines the locale of the application based on a URL that has an optional locale path.

西班牙 www.website.com/es/search 对于英语 www.website.com/search For Spanish www.website.com/es/search For English www.website.com/search

/ ES 需要西​​班牙语,但英语是由参数缺少暗示,我会preFER不有 / EN

the '/es' is required for Spanish, but English is implied by the parameter missing and I would prefer to not have the '/en'.

我想这其中的任何一个孩子的状态,以继承该区域的价值。

I want any child state of this one to inherit that locale value.

$stateProvider
    .state('localization', {
        abstract: true,
        url: '/:locale?',
        params: {
            locale: { value: 'en' }
        }
    })
    .state('search', {
        url: '/search',
        parent: 'localization'
    });

我希望能在任何子状态使用 $ stateParams.locale

推荐答案

有几乎相同的Q&安培;答:角JS - 路由的UI添加默认parmeter 或的另一个这里

There is almost the same Q & A: Angular js - route-ui add default parmeter or another here

解决方案是创建一个像这根状态:

The solution is to create root state like this:

.state('localization', {
    url: '/{locale:(?:en|es|cs)}',
    abstract: true,
    template: '<div ui-view=""></div>',
    params: {locale : { squash : true, value: 'en' }}
})

任何小孩状态,则可以只使用此作为父:

Any child state then can just use this as a parent:

.state('home', {
    parent: 'localization', // parent will do the magic
    url: "/",
    ...
})
.state('activity', {
    parent: 'localization',
    ...

这里快来看,这里也完全工作plunker

Check it here, where is also fully working plunker