Question

There's a solution to create routes for subdomains with AngularJS?

I searched on google and many others and I cant solve this problem.

I want do like "http://minetop.net/serverName"

app.config(function($routeProvider)
{

    $routeProvider

    .when('/:serverID', {
        templateUrl : '../design/template/pages/server.html',
        controller  : 'srvCtrl'
    })          

    .when('/', {
        templateUrl : '../design/template/pages/home.html',
        controller  : 'mainCtrl'
    }); 

});

But I want do this with subdomains, example : http://serverName.minetop.net

PS : "serverName" is dynamic.

Was it helpful?

Solution

Unfortunately as far as the Angular router is concerned subdomains are not even a part of your app. In order to redirect to a subdomain you need to do a few things.

1) modify the /private/etc/hosts file to include the subdomain. From the command line

sudo vi /private/hosts/etc

then add the subdomain next to your localhost alias. This will allow your local resolve the subdomain to your app

2) create a route filter that will do a redirect to your subdomain given certain conditions. In order to actually redirect just use

myRedirectionFilter = function(){
if("foo" !== "bar"){
 window.location.href = "http://serverName.minetop.net"}
}

3) use the filter in the resolve function in your routes.js file.

.when('/', {
    templateUrl : '../design/template/pages/home.html',
    controller  : 'mainCtrl',
    resolve: myRedirectionFilter()
}); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top