简体   繁体   English

如何在没有HTTP Get on Angular JS的情况下读取JSON文件?

[英]How to read JSON file without HTTP Get on Angular JS?

I currently I have this way to get a JSON list. 我目前有这种方法来获取JSON列表。

function ()
{
    return $http
    (
        {
            url: '../Content/Scripts/Languages/en.json',
            method: "GET",
            headers:
            {
                'Content-Type': 'application/json; charset=utf-8'
            }
        }
    );
},

The reason I don't want to use a GET method is because it works as a promise, and because of that, any function that happens after the promise will execute, and if the list that I retrieve from there is empty will make the app crash. 我不想使用GET方法的原因是因为它作为一个Promise起作用,因此,在Promise之后执行的任何功能都将执行,并且如果我从那里检索到的列表为空,则会使该应用程序崩溃。

So what I want is retrieve the list and after that, the rest of my functions can execute with a List filled. 所以我想要的是检索列表,然后,其余功能可以在填充列表的情况下执行。

Any suggestions? 有什么建议么?

You can use RequireJS for this. 您可以为此使用RequireJS

You will need just this: 您将需要以下内容:

define('yourModuleWithNgApplication', ['json!path/to/json/file.json'], function (json) {
   // here you can access json variable, it will contain content of file.json
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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