6

I'm working with angularjs and UI-Router. I'd like to configure routes which specify the selected language. though this part of the route should be optional.

I have following states:

{
    state: 'app',
    config: {
          abstract: true,
          url: '/{lang:(?:de|en)}',
          template: '<ui-view/>'
    }
}

{
    state: 'app.mainview',
    config: {
          url: '/mainview',
          templateUrl: 'app/mainview/mainview.html',
          controller: 'MainviewController',
          controllerAs: 'vm',
          title: 'Main View',
          settings: {
              pos: 1,
              displayName: 'Mainview',
              icon: 'code-array'
          }
    }
}

now its only possible to navigate to mainview with

example.com/en/mainview

Though I'd like to configure the ui-router so that all o the following routes are valid:

example.com/mainview
example.com/en/mainview
example.com/de/mainview

Can I set a route with an optional language param at the beginning without using a double slash? If no, what alternatives do you suggest?

1 Answer 1

5

There is a solution in detail described here

where you basically introduce parent state

.state('root', {
    url: '/{lang:(?:en|de|cs)}',
    abstract: true,
    template: '<div ui-view=""></div>',
    params: {lang : { squash : true, value: 'en' }}
})
Sign up to request clarification or add additional context in comments.

1 Comment

how i can change lang in any state without change it as example from /en/hello to /de/hello without reload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.