I am new to Angularjs. I am creating some employee app which basically fetch the data from http call(json)
and display it using angularjs.
The problem is before getting the response from http
call my view got executed, so nothing displayed in my output page. I know there should be some logic to handle the async call but i am not aware of that, so could someone please help me?
Home.html
<html ng-app="employeeApp">
<head>
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<meta charset="utf-8">
<title>Employee Example</title>
<script
src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
<script>
var employeeApp = angular.module('employeeApp', []);
employeeApp.controller('EmployeeCtrl', ['$http','$scope',
function(http, scope) {
http.get('http://localhost:9999/employee/7').success(
function(data) {
scope.employees = data;
});
} ]);
</script>
</head>
<body ng-controller="EmployeeCtrl">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
<th>LOCATION</th>
</tr>
<tr ng-repeat="employee in employees">
<td>{{employee.id}}</td>
<td>{{employee.name}}</td>
<td>{{employee.age}}</td>
<td>{{employee.location}}</td>
</tr>
</table>
<hr>
<hr>
</body>
</html>
Actual output:
My Expected output:
Note:
If i load from json file or run from web browser i got the expected output but its not working in Angular script.
http://localhost:9999/employee/7
{"id":7,"age":65,"name":"test","location":"singapore"}
.catch(function(error){console.log('error', error);})
to your http promise to make sure to catch any errors