Angular 获取 JSON 文件显示为空 [英] Angular get to JSON file displaying as empty
本文介绍了Angular 获取 JSON 文件显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 angular 的新手,并试图将它集成到我的应用程序中.我正在尝试使用一个简单的 $http.get
到一个 .JSON 文件,它似乎被找到了,但是当试图将数据传递到前端,或者甚至提醒它时,它是否在提醒作为 []
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 []
这是我的:
$scope.countries = [];
$http.get('/resources/data/countries-report.json', function(data){
$scope.countries = data;
}, function(error) {
//handle error here
});
alert(JSON.stringify($scope.countries));
这是我的 .JSON 文件:
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" }
]
}
推荐答案
试试这个:
$scope.countries = [];
$http.get('/resources/data/countries-report.json', function(data){
$scope.countries = data;
alert(JSON.stringify($scope.countries));
}, function(error) {
//handle error here
});
警报应该进入回调.
这篇关于Angular 获取 JSON 文件显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文