2017-05-04 46 views
0

我控制器说:NG-重复无法检索对象的数组数据

app.controller("PatientsController",function ($scope, $sce, $http,$window,$timeout){ 
      var myScope = $scope; 
      myScope.PatientList = []; 
      myScope.getPatients = function() { 
       $http.get('http://my-ip/dashboard/patientlist').then(function(data){ 
        myScope.PatientList = data.data; 
        console.log(myScope.PatientList); 

       }); 
      }; 
      myScope.getPatients(); 
     }); 

的console.log有:

[{ 
    "appointmentcount": "7", 
    "name": "A", 
    "phonenumber": "B", 
    "dateregistered": "2017-01-04 15:20:08" 
}, { 
    "appointmentcount": "27", 
    "name": "C", 
    "phonenumber": "D", 
    "dateregistered": "2017-01-04 15:43:51" 
}] 

尝试使用NG-重复是不会放弃任何输出进行检索:

<table class="table table-bordered table-condensed table-striped"> 
<div ng-repeat="pp in PatientList"> 
<tr> 
<td>{{pp.name}} </td> 
</tr> 
</div> <!-- div ends --> 
</table> 

任何想法我错过了什么?

使用http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js

+0

你有'NG-app'和'NG-controller'正确设置在HTML? – Sajal

回答

2

问题是与你的表标签,做tr标签,而不是DIV的NG-重复。还请确保您已添加ng-appng-controller

<table> 
     <tr ng-repeat="x in PatientList"> 
     <td>{{ x.name }}</td> 
     <td>{{ x.phonenumber }}</td> 
     </tr> 
</table> 

DEMO

var app = angular.module("myApp", []); 
 
app.controller("PatientsController", function($scope) { 
 
var myScope = $scope; 
 
myScope.PatientList = []; 
 
myScope.PatientList = [{ 
 
    "appointmentcount": "7", 
 
    "name": "A", 
 
    "phonenumber": "B", 
 
    "dateregistered": "2017-01-04 15:20:08" 
 
}, { 
 
    "appointmentcount": "27", 
 
    "name": "C", 
 
    "phonenumber": "D", 
 
    "dateregistered": "2017-01-04 15:43:51" 
 
}]; 
 
});
<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<body ng-app="myApp" ng-controller="PatientsController"> 
 
<table> 
 
    <tr ng-repeat="x in PatientList"> 
 
    <td>{{ x.name }}</td> 
 
    <td>{{ x.phonenumber }}</td> 
 
    </tr> 
 
</table> 
 
</body> 
 
</html>

+1

你刚刚救了我一命,并赢得了一个啤酒男人:)谢谢你 – Satya

+0

这是真的。下一次复制您生成的html而不仅仅是模板。 – benams

+0

@Sajeetharan,标记。谢谢 – Satya

1

你不应该在<div>使用ng-repeat一个<table>内。您可能需要重复<tr>

<table class="table table-bordered table-condensed table-striped"> 
    <tr ng-repeat="pp in PatientList"> 
    <td>{{pp.name}}</td> 
    </tr> 
</table> 

Working JSFiddle