[英]Assign a value in AngularJS Provider from a remote server
I need help on how to retrieve a JSON string from a remote server and assign that string to a variable in AngularJS. 我需要有关如何从远程服务器检索JSON字符串并将该字符串分配给AngularJS中的变量的帮助。 All I am given is a url to the server where the JSON data is being generated.
我所得到的只是到生成JSON数据的服务器的URL。 For example, given the url
http://{{remotedomain}}/message/get/zh_cn
, the browser will display the JSON message as shown below: 例如,给定URL
http://{{remotedomain}}/message/get/zh_cn
,浏览器将显示JSON消息,如下所示:
{"login":"\u767b\u5165","username":"\u5e10\u53f7","password":"\u5bc6\u7801","enter_username":"\u8f93\u5165\u5e10\u53f7","enter_password":"\u8f93\u5165\u5bc6\u7801","user-not-found":"\u5e10\u53f7\u4e0d\u5b58\u5728","user-banned":"\u5e10\u53f7\u7981\u7528","user-found":"\u5e10\u53f7\u6b63\u786e","online":"\u5728\u7ebf","exchange_rate":"\u6c47\u7387","home":"\u9996\u9875","file":"\u6587\u6863","assistant":"\u5e2e\u624b","communicate":"\u4ea4\u6d41","personnel":"\u4eba\u4e8b","system":"\u7cfb\u7edf","others":"\u66f4\u591a","logout":"\u767b\u51fa","shipping":"\u51fa\u8d27","bom":"BOM","nonbom":"\u975eBOM","producing":"\u751f\u4ea7","warehouse":"\u4ed3\u5e93","finance":"\u8d22\u52a1","audiction":"\u5ba1\u6838","sales":"\u4e1a\u52a1"}
So my question really is, how can I assign the JSON data shown to a "message" variable of the AngularJS provider service shown below? 所以我的问题确实是,如何将显示的JSON数据分配给下面显示的AngularJS提供程序服务的“消息”变量?
angular.module('myApp', [])
.provider('translator', function(){
var messages = //get from the remote
defaultLanguage = 'en';
...
Providers are used during the configuration phase. 在配置阶段使用提供程序。 So, most services if not all are not available during this time.
因此,大多数服务(如果不是全部)在此期间不可用。
But, this might work: 但是,这可能有效:
function TheProvider() {
var messages;
this.$get = ['$resource', function($resource){
message = // use $resource somehow
}];
}
You might need to check $decorator . 您可能需要检查$ decorator 。 I think that's what you really need.
我认为这就是您真正需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.