从远程服务器在AngularJS Provider中分配值

时间:2014-08-19 02:13:06

标签: javascript json angularjs

我需要有关如何从远程服务器检索JSON字符串的帮助,并将该字符串分配给AngularJS中的变量。我给出的只是生成JSON数据的服务器的URL。例如,给定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"}

所以我的问题是,如何将显示的JSON数据分配给"消息"下面显示的AngularJS提供者服务的变量?

angular.module('myApp', [])
    .provider('translator', function(){
        var messages = //get from the remote 
            defaultLanguage = 'en';
                ...

1 个答案:

答案 0 :(得分:0)

在配置阶段使用提供程序。因此,在此期间,大多数服务(如果不是全部)都不可用。

但是,这可能会奏效:

 function TheProvider() {

     var messages;

     this.$get = ['$resource', function($resource){
           message = // use $resource somehow
     }];
 }

您可能需要检查 $ decorator 。我认为这就是你真正需要的。