简体   繁体   English

如何在我的AngularJS控制器中获取特定于环境的URL?

[英]How can I get an environment specific URL into my AngularJS controller?

Situation 情况

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. 这还不错,它可以工作。

The Problem 问题

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")获得以下信息:

  • On http://example.com/ : => /Contest/GetUpdates http://example.com/ = = /Contest/GetUpdates
  • On http://localhost/SubDir : => /SubDir/Contest/GetUpdates http://localhost/SubDir :=> /SubDir/Contest/GetUpdates

The Question 问题

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. 然后,您可以注入您的主应用程序。

Here's a Plunker 这是一个笨蛋

// 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.

相关问题 AngularJS-如何在控制器中使用通过Factory GET请求检索的数据? - AngularJS - How can I use the data retrieved using a Factory GET request in my controller? 如何在angularjs中将数据从工厂传递到我的控制器? - How can I pass an data from factory to my controller in angularjs? 如何从mongodb获取值到angularjs控制器 - How can I get a value from mongodb to an angularjs controller 如何从javascript函数获取ID到angularJS控制器 - How can I get the ID from a javascript function to an angularJS controller 如何从控制器{angularJS}到脚本{js}获取值 - how can I get a value from controller{angularJS} to script{js} Angular.js-如何从URL获取参数 - Angularjs - How can I get parameters from URL 给定AngularJS中的模板和控制器实例,如何使用它的控制器实例编译模板? - Given a template and a controller instance in AngularJS, how can I compile the template in a way that it uses my controller instance? 如何从需要URL的AngularJS控制器访问嵌入式资源(.html)? - How can I access an embedded resource (.html) from an AngularJS controller that expects a URL? 如何在文档头中使用AngularJS控制器特定的作用域值? - How can I use AngularJS controller-specific scope values in document head? 如何使用RegEx获取URL的特定部分? - How can I get a specific part of a URL using RegEx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM