Many Angularjs Routeprovider routes lead to the same view

So the workflow with angularjs $ routeProvider goes like this:

    $routeProvider
    .when('/main1', {
        templateUrl: 'view1.html',
    })
    .when('/main2', {
        templateUrl: 'view2.html',
    })

      

My question is if there is a way to simplify the following code. If when ('/ main1') and when ('/ main2') point to the same pattern. Thus,

    $routeProvider
    .when('/main1', {
        templateUrl: 'view1.html',
    })
    .when('/main2', {
        templateUrl: 'view1.html',
    })

      

The question is asked because if we have multiple languages ​​on the site and we want to have multiple URL translations.

Another solution would be to find out if the site is using, for example, .com or .de, and thus correct the translation of / main 1 or / main2. For example,

var url = window.location.href;
var main;
if (url.match(/.de/) !== null){
    main = "/main1";
}else{
   main = "/main2";
}

$routeProvider
.when(main, {
    templateUrl: 'view1.html',
})

      

But semantically this doesn't seem like the best solution, as I like to keep the config options set in the startup block after config. We also cannot inject factories (only providers, I might be wrong) in config.

+3


source to share


1 answer


I would like to put the language as the first segment of the url in the route.

$routeProvider.when('/:lng/main', {
    templateUrl: 'main.html',
    controller: function($routeParams){
        var lng = $routeParams.lng;
    }    
})

      



It would be very nice though to $routeProvider

provide this functionality where the URL segment can be isolated. I'm not that handsome putting :lng

in every route.

0


source







All Articles