我正在使用具有ajax的Angular Js但是使用ng-repeat无法从json数组获取输出。在警报获取[对象] ...等等但使用$scope.names= response[0].id;
时,在警报时获取“1”id。我需要在表格中获取所有数据。是否有任何方法可以获取表中的所有数据,而且我没有在控制器中使用$http
因为它不能在移动浏览器中工作但在桌面上工作,我不知道为什么?请帮助!
JS
var app = angular.module('studentApp',[]);
app.controller('StudentCntrl', function($scope){
$.ajax({
url : '/fetchAllData',
type : 'GET',
success : function(response){
$scope.names=response;
//alert($scope.names);(This is working)
}
});
});
JSP
<div ng-app="studentApp" ng-controller="StudentCntrl">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>EMAIL</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in names">
<td>{{x.id}}</td>
<td>{{x.name}}</td>
<td>{{x.email}}</td>
</tr>
</tbody>
</table>
</div>
的的Json
[{"id":"1","name":"abdul","email":"a@gmail.com"},{"id":"2","name":"pratyush","email":"p@gmail.com"},{"id":"3","name":"ankit","email":"a@gmail.com"},{"id":"45","name":"kjhj","email":"kjhkj"},{"id":null,"name":null,"email":null},{"id":null,"name":null,"email":null},{"id":null,"name":null,"email":null},{"id":"trffs","name":null,"email":null},{"id":"afa","name":"sdgfdsg","email":"dsagdsg"},{"id":"12","name":"pppp","email":"hjk,gh"}]
答案 0 :(得分:1)
使用带有角度的$http
而不是ajax调用
var app = angular.module('studentApp', []);
app.controller('StudentCntrl', function($scope,$http) {
$http.get('data.json').then(function (response){
console.log(response.data.pages);
$scope.names = response.data;
});
});
以下是工作 DEMO
答案 1 :(得分:1)
使用$ ajax调用更新范围(您应该使用$ http)时,必须使用$ scope apply来更新绑定。
$scope.$apply(function(){
$scope.names = response.data;
});