Because you cant access services inside .config
how would I get access to properties like $host
normally found in $locationService
?
I am trying to load partials based on current subdomain. All subdomains point to base host doc root.
When viewing:
example
templateUrl would be:
views/home.html
When viewing:
spicy.example
templateUrl would be:
views/spicy/home.html
Because you cant access services inside .config
how would I get access to properties like $host
normally found in $locationService
?
I am trying to load partials based on current subdomain. All subdomains point to base host doc root.
When viewing:
example.
templateUrl would be:
views/home.html
When viewing:
spicy.example.
templateUrl would be:
views/spicy/home.html
Share
Improve this question
asked Jun 2, 2013 at 0:42
Dan KanzeDan Kanze
18.6k28 gold badges84 silver badges135 bronze badges
2 Answers
Reset to default 7In the app.run method, you can use $routeChangeStart event to intercept just before route is executed. In your .config, specify the templateURL as the file of the template itself (e.g home.html, without /views)
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.html',
controller: 'HomeCtrl'
});
...
});
and then you can add the following code in the .run:
app.run(function ($rootScope,$location){
$rootScope.$on('$routeChangeStart',function(event,next,current){
next.templateUrl = "views/"+$location.host()+"/"+next.templateUrl;
});
});
and it will access home.html under views/<host>/home.html
Plunker example: http://embed.plnkr.co/82qV7uCZOBgZxjahhlU3/ Please note I use _ instead of / to simulate directory where the file resides based on the hostname. Plunker doesn't like / in the filename (refused to save the file although I was able to run it)
You can access $$host in config. You just can't use $location. Instead use $urlRouterProvider. However, $urlRouterProvider populates $location as empty for some reason, so you can also use pure javascript window to get the url host if you are changing your config env based on different url hosts.
E.g.
function config($urlRouterProvider) {
if ($urlRouterProvider._router.locationService.$location)
console.log(console.log($urlRouterProvider._router.locationService.$location.$$host))
else
console.log(window.location.host)
}