Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: optional language param into url

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?

like image 545
Ich Avatar asked Sep 23 '15 13:09

Ich


1 Answers

There is a solution in detail described here

  • Angular js - route-ui add default parmeter
  • Prepend optional attribute in angular ui-router URL

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' }}
})
like image 80
Radim Köhler Avatar answered Oct 02 '22 06:10

Radim Köhler