我可以将数据发送到表格,但我无法检索数据。这是 我试过的代码。我想在html页面中显示该表的详细信息。 我无法理解为什么这段代码不起作用。有人可以帮忙吗?
php代码:
<?php
$con = mysqli_connect('localhost','root','','hamatkin');
if(!$con){
die("couldnt connect".mysqli_error);
}
$query = "SELECT * FROM customers";
$result = $con->query($query);
$r = array();
if( $result->num_rows>0){
while($row = $result->fetch_assoc()){
$r[] = $row;
}
}
$res = json_encode($r);
echo $res;
?>
控制器:
"use strict";
var app = angular.module('dataSystem',[]);
app.controller('customerListCtrl',function($scope,$http){
$http({method:'GET', url:'get-allCustomers.php'}).success(function(response){
$scope.customers = response;});
});
});
HTML:
<div>
<table ng-controller="customerListCtrl" >
<tr ng-repeat="x in customers">
<td> {{ x.customer_id}} </td>
<td> {{ x.first_name }} </td>
<td> {{ x.last_name }} </td>
<td> {{ x.id}} </td>
<td> {{ x.city}} </td>
<td> {{ x.adress}} </td>
<td> {{ x.phone}} </td>
<td> {{ x.email}} </td>
<td> {{ x.fax}} </td>
<td> {{ x.referrer}} </td>
<td> {{ x.comments}} </td>
</tr>
</table>
</div>
答案 0 :(得分:1)
您必须使用response.data
而不是response
重要:
$ http遗留承诺方法成功与错误 弃用。请改用标准方法。如果 $ httpProvider.useLegacyPromiseExtensions设置为false然后这些 方法将抛出$ http / legacy错误。
"use strict";
var app = angular.module('dataSystem',[]);
app.controller('customerListCtrl',function($scope,$http){
$http({method:'GET', url:'get-allCustomers.php'}).then(function(response){
$scope.customers = response.data;
}, function(response){
//Your errorhandler
});
});
为了提高可读性,您还可以使用较短版本的get请求:
$http.get('get-allCustomers.php').then(function(response){
$scope.customers = response.data;
}, function(response){
//Your errorhandler
});
答案 1 :(得分:-1)
请尝试
<?php
$con = mysqli_connect('localhost','root','','hamatkin');
if(!$con){
die("couldnt connect".mysqli_error);
}
$query = "SELECT * FROM customers";
$result = $con->query($query);
$r = array();
if( $result->num_rows>0){
while($row = $result->fetch_assoc()){
$r[] = $row;
}
}
$res = json_encode($r);
echo $res;
die();
?>
添加die();功能