Dynamic subdomain with angular

I am new to Angularjs and I would like to add a dynamic subdomain such as sub.domain.com. By changing sub, I could request the correct data from the server. However, the home page will still be the same.

sub1.domain.comand sub2.domain.comwill have the same page home.tpl.html, except that the returned data will be domain specific.
This will be the same for other pages.

Is there any way to do this?

+4
source share
3 answers

The main thing you need to look at is the $locationservice , in particular the method $location.host(). This will return you the full domain, from there it is easy to get a subdomain and use it.

, , .

- :

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

, ..

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

, angular, , -- .

U 3 , :

  • ru.example.com( ) |
  • de.example.com( ) | → example.com
  • it.example.com( ) |

.com, - , , i18n ( angular).

, - en.example.com( example.com, angular), angular (en , . ) angular.

+2

Why is there no point-breaking and counting of host () elements?

var host = $location.host();
var parts = host.split('.');
var subdomain = null;

// more than domain.cn, will always return the first
if (parts.length > 2)
  subdomain = parts[0];

return subdomain;

hope this helps!

+2
source

All Articles