AngularJS使用HTTP获取JSON

angularjs fetching json using http

本文关键字:JSON 获取 HTTP 使用 AngularJS      更新时间:2023-09-26

我是 angularjs 的新手,

这是我的代码:

<!DOCTYPE HTML>
<html ng-app="myapp">
<head>
<meta charset="utf-8">
<title>Angularjs project</title>
<script type="text/javascript" src="angular.min.js"></script>
<script>
var myapp=angular.module('myapp',[]);
myapp.controller('myctrl', function ($scope,$http) {
$http.get('countries.json').success(function(data){
$scope.countries=data;
});
});
</script>
<body>
<div ng-controller="myctrl">
<table>
<tr>
<th>country</th>
<th>population</th>
</tr>
<tr ng-repeat="country in coutries">
<td>{{country.name}}</td>
<td>{{country.population}}</td>
</tr>
</table>
</div>
</body>
</html>

我有一个 json 文件,当它运行时,它不起作用。

当我在谷歌中提到时,他们说的是服务器.js或安装节点.js类似的东西。

只是困惑,我应该遵循什么,任何人都可以指导我获取json数据并显示。

任何帮助将不胜感激。

提前致谢。

拼写

错误

$scope.countries=data;
<tr ng-repeat="country in coutries"> 

更改为 :

<tr ng-repeat="country in countries">

你必须包含角度.js脚本。在代码中找不到它。此外,country.json 文件必须与项目结构中的上述 html 处于同一级别。