的延續FO這個問題導致this question,但得到的答覆是同時適用於:
我決定去與本地stradegy有兩個原因:
- 沒有額外的XML請求開銷。
- 當資源不存在時,404消息不會監視控制檯日誌。
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
}
};
});
您的模板url看起來只是您的索引文件路徑的相對路徑。所以要更改域名(小心同源策略),您必須使這些路徑爲絕對路徑,可能使用常量來配置它們。 –
@xmltechgeek對不起,但我想你可能跳過了子域指向同一個doc根的部分--- SOP與它無關。這裏的問題是,我需要一種檢測域的方法,而不是攔截「templateUrl」並將其更改爲不同的子域。 –
不知道你會用默認的角度路由器做到這一點。你應該可以使用angular-ui團隊的ui-router(https://github.com/angular-ui/ui-router)中的templateProvider來做到這一點。請參閱https://github.com/angular-ui/ui-router/wiki#alternative-ways-to-set-the-template。 –