简体   繁体   中英

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. All I am given is a url to the server where the JSON data is being generated. For example, given the url http://{{remotedomain}}/message/get/zh_cn , the browser will display the JSON message as shown below:

{"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?

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 . I think that's what you really need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM