NG-重复无法检索对象的数组数据
问题描述:
我控制器说: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
答
问题是与你的表标签,做tr标签,而不是DIV的NG-重复。还请确保您已添加ng-app和ng-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>
答
你不应该在<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>
你有'NG-app'和'NG-controller'正确设置在HTML? – Sajal