使用angularjs的动态子域 [英] dynamic subdomain with angularjs

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

问题描述

我是Angularjs的新手,我想添加动态子域,例如sub.domain.com. 通过更改sub,我将能够从服务器询问正确的数据.但是,主页仍然是相同的.

I am new to Angularjs and I would like to add dynamic subdomain such as sub.domain.com. By changing sub, I would be able to ask the proper data from the server. However, the home page will still be the same.

sub1.domain.comsub2.domain.com将具有相同的home.tpl.html页,但返回的数据将特定于域.
其他页面也一样.

sub1.domain.com and sub2.domain.com will have the same home.tpl.html page except that the data returned will be specific to the domain.
It will be the same for other pages too.

有办法吗?

推荐答案

您需要查看的主要内容是 $location.host()方法.这将为您返回完整的域,从那里很容易获得子域并使用它.

The main thing you need to look at is the $location service, in particular the $location.host() method. That will return you the full domain, from there it is easy to get the subdomain and use that.

您甚至可以提供一项非常简单的服务,您可以将其注入任何地方以轻松访问子域.

You could even do a really simple service that you could inject anywhere to get access to the subdomain easily.

类似的东西:

app.factory('subdomain', ['$location', function ($location) {
    var host = $location.host();
    if (host.indexOf('.') < 0) 
        return null;
    else
        return host.split('.')[0];
}]);

然后,您可以在控制器,指令等中将其用作简单的可注入值.

Then you can use this as a simple injectable value in your controllers, directives, etc.

app.controller('SomeCtrl', ['$scope', 'subdomain', function ($scope, subdomain) {
     // use subdomain same as any other variable
}]);

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆