[英]How can I get an environment specific URL into my AngularJS controller?
Our web project in production has a root URL like so: http://example.com
. 我们生产中的Web项目的根URL如下所示: http://example.com
: http://example.com
。 Our local dev instances (don't ask me why) default to a URL like so: http://localhost/SubDir
where SubDir is a placeholder for the actual virtual subdirectory in IIS. 我们本地的dev实例(不要问我为什么)默认使用如下URL: http://localhost/SubDir
,其中SubDir是IIS中实际虚拟子目录的占位符。
This is not the problem. 这不是问题。 As much as I would like to abolish that subdirectory URL thing, that is only illustrating a problem. 尽管我想废除该子目录URL的内容,但这仅说明了一个问题。 If we were to move to a different domain or URL later on that incorporates a subdirectory URL, it would not be a problem for all of the instances in code where we use helpers to generate URLs (eg @Url.Action("GetUpdates")
, etc). 如果稍后我们要移动到包含子目录URL的其他域或URL,那么对于使用助手生成URL的代码中的所有实例来说,这都不是问题(例如@Url.Action("GetUpdates")
等)。
Currently we have quite a bit of javascript that has been moved to external .js files that uses jQuery to get URL strings from hidden inputs whose values have been set to a Url helper like above. 当前,我们已经将大量的javascript移至使用jQuery从隐藏输入中获取URL字符串的外部.js文件中,这些隐藏输入的值已设置为上述Url助手。 This is not too bad, it works. 这还不错,它可以工作。
AngularJs. AngularJs。 It is not the problem. 这不是问题。 It has however uncovered the problem. 但是,它已经发现了问题。 We want to remove jQuery from the project eventually. 我们最终希望从项目中删除jQuery。 Right now, I am getting the URLs in my controller using jQuery (I am willing to break rules in an effort to get something out on time if I can be reasonably assured I will be given time to fix it later). 现在,我正在使用jQuery在我的控制器中获取URL(我愿意打破规则,以便可以保证我能有时间稍后修复它,以便按时解决问题)。 It is now time to fix it, and I am at a loss. 现在该修复它了,我很茫然。
Between the two environments, I get the following from @Url.Action("GetUpdates")
: 在这两种环境之间,我从@Url.Action("GetUpdates")
获得以下信息:
http://example.com/
: => /Contest/GetUpdates
在http://example.com/
= = /Contest/GetUpdates
http://localhost/SubDir
: => /SubDir/Contest/GetUpdates
在http://localhost/SubDir
:=> /SubDir/Contest/GetUpdates
How can I get an environment dependent URL into my AngularJs controller without the use of the helpers in that controllers file? 如何在不使用该控制器文件中的帮助程序的情况下,将与环境有关的URL放入AngularJs控制器中? I originally thought it might be as simple as using the hidden inputs and setting the ng-model, but does not seem to have worked. 我原本以为它可能和使用隐藏的输入并设置ng-model一样简单,但似乎没有用。 I have also looked into using an extension, RazorJS, but hoped that there would be a better way of doing it more inline with the Angular paradigm. 我也研究过使用扩展RazorJS,但希望有更好的方法使其更符合Angular范式。
Anyone have any ideas? 有人有想法么? How do you get URLs into your controllers? 您如何将URL导入控制器?
If you are able to change your environment config server side you can put the variable inline in a module constant. 如果您能够更改环境配置服务器端,则可以将变量内联到模块常量中。 Then you can inject into your main app. 然后,您可以注入您的主应用程序。
// inline
angular.module('Preload',[]).contstant('CONFIG', {'BASE_URL':'http://localhost/foo'});
// inject
var app = angular.module('MainApp', ['Preload']);
// Controller
app.controller('MainCtrl', function($scope, CONFIG) {
$scope.base_url = CONFIG.BASE_URL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.