这个问题的延续导致了这个问题,但答案适用于两者:
我决定采用本地策略有两个原因:
- XML 请求没有额外的开销。
- 当资源不存在时,404 消息不会污染控制台日志。
服务.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;
}).
控制器.js
controller('AppController', ['$scope','Views', function($scope, Views) {
$scope.$on("$routeChangeSuccess",function( $currentRoute, $previousRoute ){
$scope.page = Views.returnView();
});
}]).
过滤器.js
filter('objExists', function () {
return function (property) {
try {
return property;
} catch (err) {
return null
}
};
});