Dynamic routing by sub domain with AngularJS

后端 未结 2 612
天命终不由人
天命终不由人 2021-02-11 04:53

How do I override template URL based on sub domain?

All of my sub domains point to the same doc root.

Base level domain: example.com

2条回答
  •  没有蜡笔的小新
    2021-02-11 05:05

    Easy and clean: I would inspect window.location in the function that sets up your routes, set a variable depending on the subdomain, then use that variable when setting up the routes. As in:

    var isSubDomain = window.location.host.indexOf("sub") == 0
    var urlPath = isSubDomain ? "sub.example.com" : "example.com";
    
    ...
    
    $routeProvider.when('/', {templateUrl: 'views/' + urlPath + '/home.html'});
    

    TL;DR: use JavaScript, not Angular

提交回复
热议问题