本文介绍了用AngularJS子域动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何基于子域覆盖模板网址是什么?

How do I override template URL based on sub domain?

我所有的子域指向同一个文档根。

基础顶级域名: example.com

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

子域: sub.example.com

$routeProvider.when('/', {templateUrl: 'views/sub.example.com/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.

推荐答案

一个延续FO这个问题导致的 ,但答案为是适用的:

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

我决定与当地stradegy走有两个原因:

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


  1. 有XML请求没有额外的开销。

  2. 404的消息惯于polute控制台日志时资源不存在。

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
    }
  };
});

这篇关于用AngularJS子域动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 04:43