I've just started learning angularJS and am trying to figure out why the following code doesn't work
<html>
<head>
<title>Angular JS Ajax</title>
</head>
<body>
<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>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/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>
</body>
</html>
When I change the attribute of ng-app to "" (instead of "mainApp"'), and replace the` code above with the following, the application works.
I'd really appreciate if anyone could explain to me why this is the case.
<script>
function studentController($scope, $http){
var url = "data.txt";
$http.get(url).success(function(response){
$scope.students = response;
});
};
</script>
Here's the data.txt file:
[
{
"Name": "Mahesh Parashar",
"RollNo": 101,
"Percentage": "80%"
},
{
"Name": "Dinkar Kad",
"RollNo": 191,
"Percentage": "75%"
}
]
Thank you!