admin管理员组文章数量:1310099
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)
}
本文标签: javascriptAccess host name inside config AngularJSStack Overflow
版权声明:本文标题:javascript - Access host name inside config AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741854463a2401258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论