AngularJS: optional language parameter in URL

I work with angularjs and UI-Router. I would like to set up routes that set the selected language. although this part of the route should be optional.

I have the following conditions:

{ 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 you can only go to mainview with

 example.com/en/mainview 

Although I would like to configure ui-router so that all o are valid:

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

Is it possible to specify a route with an optional language parameter at the beginning without using a double slash? If not, what alternatives do you offer?

+5
source share
1 answer

The solution is described in detail here

where you basically enter the parent state

 .state('root', { url: '/{lang:(?:en|de|cs)}', abstract: true, template: '<div ui-view=""></div>', params: {lang : { squash : true, value: 'en' }} }) 
+3
source

Source: https://habr.com/ru/post/1232100/


All Articles