angularjs 使用 http 获取 json

angularjs fetching json using http

我是 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 文件,当它 运行 时,它不起作用。

当我在 google 中提及时,他们说 server.js 或安装 node.js 类似的东西。

我很困惑,我应该遵循什么,谁能指导我获取 json 数据并显示。

任何帮助将不胜感激。

提前致谢。,

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

拼写错误

$scope.countries=data;

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

更改为:

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