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?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Ich
  • 226
  • 3
  • 13

1 Answers1

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' }}
})
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335