我有一个非常简单的程序,它从与HTML文件位于同一文件夹中的JSON文件(data.txt)中读取数据。但程序无法读取此文件,这是生成的浏览器输出。
浏览器输出
Name Roll No Percentage
{{ student.Name}} {{ student.RollNo}} {{ student.Percentage}}
main.html中
<!doctype html>
<html>
<head>
<title>AngularJS Ajax</title>
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f2f2f2;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function ($scope, $http) {
var url = "data.txt";
$http.get(url).success(function (response) {
$scope.students = response;
});
}
</script>
</head>
<body>
<h2>AngularJS Ajax</h2>
<div ng-app="mainApp" ng-controller="studentController">
<table>
<tr>
<th>Name</th>
<th>Roll No</th>
<th>Percentage</th>
</tr>
<tr ng-repeat="student in students">
<td>{{ student.Name }}</td>
<td>{{ student.RollNo }}</td>
<td>{{ student.Percentage }}</td>
</tr>
</table>
</div>
</body>
</html>
data.txt中
[
{
"Name" : "Mahesh Parashar",
"RollNo" : 101,
"Percentage" : "80%"
},
{
"Name" : "Dinkar Kad",
"RollNo" : 201,
"Percentage" : "70%"
},
{
"Name" : "Robert",
"RollNo" : 191,
"Percentage" : "75%"
},
{
"Name" : "Julian Joe",
"RollNo" : 111,
"Percentage" : "77%"
}
]
答案 0 :(得分:0)
您正在获取txt文件的内容,它很可能被解释为字符串,因此您可以将学生设置为某些字符串。
相反,请尝试:
$http.get(url).success(function (response) {
$scope.students = JSON.parse(response);
});
更新
现在我考虑一下,响应是否应该具有数据属性?
$http.get(url).success(function (response) {
$scope.students = response.data;
});