2017-09-06 166 views
2

我是angularjs的新手我正在学习它。同时使用Ajax $http service的小练习应用程序,我得到了错误。下面是我用过的代码。

<script type="text/javascript"> 

var app=angular.module("angulardata",[]); 

app.controller("angulardata",["$scope","$http",function($scope,$http) 
{ 

$http.get('employee.json').then(function(response){ 

$scope.employees=response; 
}); 

}]); 


    </script> 
</head> 

<body ng-app="angulardata" ng-controller="angulardata"> 

<h2>Employee Data</h2> 
<table> 
<tr ng-repeat="emp in employees"> 
<td>{{emp.firstname}}</td> 
<td>{{emp.lastname}}</td> 
<td>{{emp.Age}}</td> 
<td>{{emp.salary}}</td> 


</tr> 
</table> 

enter image description here

+1

你确定网址到json文件是正确的? # –

+0

'$ scope.employees = response;'应该是'$ scope.employees = response.data;',同时仔细检查'.json' URL –

+0

也要确保你的文件是有效的json –

回答

2

您的JSON文件是无效的: 尝试使用此

[ 
     { "firstname":"Gunjan", "lastname":"Limbachiya", "Age":30, "salary":50000}, 
     { "firstname":"Pramod", "lastname":"Limbachiya", "Age":57, "salary":70000 }, 
     { "firstname":"Kashyap", "lastname":"Limbachiya", "Age":28, "salary":60000 }, 
     { "firstname":"Malhar", "lastname":"Limbachiya", "Age":30, "salary":220000 } 
    ] 

可以随时查询,如果你的JSON是正确的位置:https://jsonformatter.curiousconcept.com/

+0

非常感谢现在正在工作 – Gunjan