У меня есть очень простая программа, которая считывает данные из файла JSON (data.txt), расположенного в той же папке, что и файл HTML. Но программа не может прочитать этот файл, и вот вывод браузера, который генерируется.
Browser Output
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%"
}
]
что такое ошибка, проверьте консоль –
@Ali - Спасибо за идею. Обнаружена проблема после проверки консоли. Отсутствие скобок и точки с запятой в функции контроллера. – user2325154