0

I have an application which can run into two contexts -- 1) Global and 2) Client

When in global context the urls are like this mydomain.com/#login, mydomain.com/#register but when in client context the urls are like this mydomain.com/#/ClientKey/login and mydomain.com/#/ClientKey/register. In both contexts I want to open exactly the same template i.e login.html and register.html. One way to achieve this is to replicate routeProvider.when for both cases like the following

 $routeProvider.when('/:ClientKey/_login', { 
                templateUrl: 'views/login.html',
                controller: 'LoginCtrl', 
            })

 $routeProvider.when('/_login', { 
                templateUrl: 'views/login.html',
                controller: 'LoginCtrl', 
            })

My question is that is there is way to do it in a single routeProvider.when, instead of replicating it twice with only a small difference. This is important for my application because there are several such links like login, register, editProfile, changePassword etc.

1 Answer 1

1

Like this?

var routes = {
    '/_login' : { templateUrl: 'views/login.html', controller:'LoginCtrl' }
    ...
}

for (var k in routes) {
    $routeProvider
        .when(k, routes[k])
        .when('/:clientId' + k, routes[k]);
}

Edit:

You can actually do optional groups with ?, so the following might work.

$routeProvider.when(':clientId?/_login', { ... });
Sign up to request clarification or add additional context in comments.

2 Comments

yes, its better, but does angular provide something like .when(k | '/:clientId' + k, routes[k])
@ShamailaTahir, angular don't allow us to specify multiple paths with OR operator(| / ||). Optional groups using <code>?</code> is a good solution for your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.