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?
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' }}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With