ng-repeat无法从对象数组中检索数据

时间:2017-05-04 12:03:19

标签: angularjs html-table

我的控制器说:

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-repeat检索它并没有给出任何输出:

<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

2 个答案:

答案 0 :(得分:3)

问题在于你的表标签,在tr标签上执行ng-repeat而不是div。另外,请确保添加了 ng-app ng-controller

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

<强>样本

&#13;
&#13;
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"
}];
});
&#13;
<!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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您不应在ng-repeat内的<div>上使用<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