角却要JSON文件显示为空 [英] Angular get to JSON file displaying as empty

查看:112
本文介绍了角却要JSON文件显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的角度和尝试我的应用程序内将其集成。我试图用一个简单的 $ http.get 来一个以.json文件,这似乎可以找到,而是试图将数据传递到前端时,甚至提醒它,它是作为报警[]

下面我得到:

  $ scope.countries = [];
    $ http.get('/资源/数据/国家-report.json'功能(数据){
            $ scope.countries =数据;
        },功能(错误){
            //这里处理错误
    });    警报(JSON.stringify($ scope.countries));

下面是我以.json文件:

  {
  名字:乔,
  姓:布罗格斯
  国:
      {名:法国,人口:63.1},
      {名:跨度,人口:52.3},
      {名:联合王国,人口:61.8}
  ]
}


解决方案

试试这个:

  $ scope.countries = [];
$ http.get('/资源/数据/国家-report.json'功能(数据){
        $ scope.countries =数据;
警报(JSON.stringify($ scope.countries));
    },功能(错误){
        //这里处理错误
});

警报应该在回调。

I am new to angular and trying to integrate it within my application. I am attempting to use a simple $http.get to a .JSON file, which seems to be found, but when trying to pass the data to the front end, or even alert it, is it alerting as []

Here my get:

$scope.countries = [];
    $http.get('/resources/data/countries-report.json', function(data){
            $scope.countries = data;
        }, function(error) {
            //handle error here
    });

    alert(JSON.stringify($scope.countries));

Here's my .JSON file:

{
  "firstName"   : "Joe",
  "surName" : "Bloggs",
  "countries" : [
      { "name": "France", "population": "63.1" },
      { "name": "Span", "population": "52.3" },
      { "name": "United Kingdom", "population": "61.8" }
  ]
}

解决方案

Try this:

$scope.countries = [];
$http.get('/resources/data/countries-report.json', function(data){
        $scope.countries = data;
alert(JSON.stringify($scope.countries));
    }, function(error) {
        //handle error here
});

Alert should go in the callback.

这篇关于角却要JSON文件显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆