Use different routes / states for each subdomain or domain name in AngularJS / ui-router

My Angular app has many different domain names and subdomains pointing to it. I would like each of you to take a different state in my routing. How can I do it?

Let me give an example:

domainname1.com
subdomain.domainname1.com
sub2.domainname1.com 

domain2.com
sub1.domain2.com

ALL point to my application root. My routes / states might look something like this:

    $stateProvider
            .state('Dom1Sub0', {
                url: '/',
                templateUrl: 'views/s0View.html',
                controller: 'domain1Controller as user'
            })
            .state('Dom1Sub1', {
                url: '/',
                templateUrl: 'views/s1View.html',
                controller: 'domain1Controller as user'
            })
            .state('Dom1Sub2', {
                url: '/',
                templateUrl: 'views/s2View.html',
                controller: 'domainController as user'
            })

            .state('Dom2Sub0', {
                url: '/',
                templateUrl: 'views/s0View.html',
                controller: 'domain2Controller as user'
            })
            .state('Dom1Sub1', {
                url: '/',
                templateUrl: 'views/s1View.html',
                controller: 'domain2Controller as user'
            })

How can I add an if statement for each state based on a domain or subdomain?

SO FAR:

I found here the code to extract the subdomain:

app.factory('subdomain', ['$location', function ($location) { 
        var host = $location.host();
    if (host.indexOf('.') < 0) 
        return null;
    else
        return host.split('.')[0];
}]);

so I can do the same for the domain:

    app.factory('domain', ['$location', function ($location) {
var host = $location.host();
if (host.indexOf('.') < 0) 
    return host.split('.')[0];
else
    return host.split('.')[1];
}]);

and add them to my controller:

    app.controller('SomeCtrl', ['$scope', 'subdomain', function ($scope, subdomain) {
 // use subdomain same as any other variable
}]);

But how to change the state based on the value of the domain and / or subdomain?

thanks

+4

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


All Articles