在运行时使用值在AngularJs应用程序中动态设置服务器URL [英] Setting server url dynamically during runtime in AngularJs app using value

查看:50
本文介绍了在运行时使用值在AngularJs应用程序中动态设置服务器URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有本地和在线托管的应用程序.我有很多api调用,我们需要更改url的前缀.我已经设置了 url 的值,并且两个URL都已寻址,一个在本地需要,一个在线.所以我有:

I have application which is hosted locally and online. I have a lot of api calls, and we have need to change the prefix of url. I have set value url with both url addressed, one is needed locally and one online. So I have:

//.value('urlPath', '')

  .value('urlPath', 'http://online-website:8888/')

在此配置中,没有注释一秒.我的问题是,如何动态更改urlPath,因此开发人员无需在每次部署到服务器时对其进行评论和取消注释.我知道我可以使用$ location服务,如果没有,我可以使用javascript raw函数来确定网站位于哪个主机.因此,我可以使条件如下:

In this configuration, there is commented one and second not. My question is, how to change urlPath dynamically, so there is no need by developers to comment and uncomment each time they deploy to server. I know i can use $location service, and if not, I can use javascript raw function to determine which host the website is located. So I can make condition like this:

 var host = $location.host();   
    if ( host.indexOf("online-website") > -1)
       //set value to online-website;
    else
       //set value to localhost

我假设我可以在开始时以某种方式使用此代码段,如.run模块方法中那样.但是,如何通过有条件地设置Angular的值来实现这一目标呢?我的问题是值是否具有可以动态设置其值的方法,如何引用它?还是有另一种解决方案,除了创建服务或继承控制器.我有很多使用值对象的控制器,所以最好的解决方案是使用值.

I assume I can use this code snippet somehow in the beginning, like in .run module method. But how to achieve this by setting Angular's Value conditionally? My question is if value has method to set dynamically it's value, how to reference it? Or is there another solution, except creating service or inheriting controller. I have a lot of controllers which are using value object, so the best solution would be with value.

此外,如果不可能的话,值和常数之间有什么区别?

Also, if it is not possible, what is the difference between value and constant?

我想在run方法中甚至对其进行动态设置.它是在定义值之后定义的.我通过以下方式动态设置值:

I wanted to set it even dynamically in run method. it's defined after value is defined. I am setting value dynamically by:

   var host = $location.host();   
      console.log('host is: ',host);
    if ( host.indexOf("website-online") > -1)
       {urlPath = '';console.log('1');}
    else
       {urlPath = 'http://website-online:8888/';console.log('2',urlPath);}

在localhost控制台上,日志2和网站在线,但api调用将转到localhost.因此价值不变.创建它时,如何更改值以及何时动态更改它,以便在视图加载期间在每个控制器中注入值是可以的?

On localhost console logs 2 and website-online, but api call is going to localhost. So value doesn't changed. When it is created, how to change value and when dynamically, so in each controller injected value during view loading would be fine?

推荐答案

在您的示例中, urlPath 是标量.这意味着

In your example urlPath is scalar. This means that

urlPath = ...

被分配给局部变量,而'urlPath'服务保持不变.使用

is assigned to local variable, while 'urlPath' service remains intact. Use

.value('urlPath', { url: 'http://online-website:8888/' })

urlPath.url = ...

相反.

这篇关于在运行时使用值在AngularJs应用程序中动态设置服务器URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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