最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic routing by sub domain with AngularJS - Stack Overflow

programmeradmin0浏览0评论

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

$routeProvider.when('/', {templateUrl: 'views/example/home.html'});

Sub-domain: sub.example

$routeProvider.when('/', {templateUrl: 'views/sub.example/home.html'});

Partials should be indifferent to static / dynamic content. If a controller inside a partial is making service calls for data this interceptor shouldn't interfere with that.

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.

$routeProvider.when('/', {templateUrl: 'views/example./home.html'});

Sub-domain: sub.example.

$routeProvider.when('/', {templateUrl: 'views/sub.example./home.html'});

Partials should be indifferent to static / dynamic content. If a controller inside a partial is making service calls for data this interceptor shouldn't interfere with that.

Share Improve this question edited Jun 2, 2013 at 18:53 Dan Kanze asked Jun 2, 2013 at 18:47 Dan KanzeDan Kanze 18.6k28 gold badges84 silver badges136 bronze badges 3
  • Your template url looks to be just a relative path to the path of your index file. So to change domains (careful of same origin policy) you'd have to have these paths be absolute, possibly using a constant to configure them. – Paul Ryan Commented Jun 2, 2013 at 22:04
  • @xmltechgeek Sorry but I think you may have skipped over the part where the sub domains are pointing to the same doc root --- SOP has nothing to do with it. The problem here is that I need a way to detect domain, than intercept the templateUrl and change it for different sub domains. – Dan Kanze Commented Jun 2, 2013 at 22:52
  • Not sure your going to be able to do that with the default angular router. You should be able to use the templateProvider in the ui-router (github./angular-ui/ui-router) from the angular-ui team to do this though. See github./angular-ui/ui-router/…. – Paul Ryan Commented Jun 2, 2013 at 22:57
Add a ment  | 

2 Answers 2

Reset to default 3

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." : "example.";

...

$routeProvider.when('/', {templateUrl: 'views/' + urlPath + '/home.html'});

TL;DR: use JavaScript, not Angular

A continuation fo this problem lead to this question, but the answer is applicable for both:

I decided to go with a local stradegy for two reasons:

  1. There is no additional overhead of XML request.
  2. 404 messages wont polute console logs when resource doesn't exist.

services.js

factory('Views', function($location,$route,$routeParams,objExistsFilter) {

  var viewsService = {};
  var views = {
    subdomain1:{
      'home.html':'/views/subdomain1/home.html'
      },
    subdomain2:{

    },
    'home.html':'/views/home.html',
  };

  viewsService.returnView = function() {
    var y = $route.current.template;
    var x = $location.host().split(".");
    return (x.length>2)?(objExistsFilter(views[x[0]][y]))?views[x[0]][y]:views[y]:views[y];
  };

  viewsService.returnViews = function() {
    return views;
  };

  return viewsService;
}).

controllers.js

controller('AppController', ['$scope','Views', function($scope, Views) {    
  $scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
    $scope.page = Views.returnView();
  });
}]).

filters.js

filter('objExists', function () {
  return function (property) {
    try {
      return property;
    } catch (err) {
      return null
    }
  };
});
发布评论

评论列表(0)

  1. 暂无评论