Angular SQL - 缺少什么?

时间:2016-12-06 14:36:04

标签: php angularjs

我正在尝试使用Angular显示一个表,但它不会显示。有什么不对吗?代码如下。该表在源代码中就在那里,但它内部没有生成任何内容。我是Angular的新手。

前端

    <div ng-app="musicApp" ng-controller="musicCtrl">

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.SongArtist }}</td>
    <td>{{ x.SongAlbum }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('musicApp', []);
app.controller('musicCtrl', function($scope, $http) {
   $http.get("http://rhys-bennett.uk/AIDc3391265/includes/formatjson.php")
   .then(function (response) {$scope.names = response.records;});
});
</script>

PHP后端

    <?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");


$result = $conn->query("SELECT song_name, song_artist, song_album FROM musictable");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= '{"Name":"'  . $rs["song_name"] . '",';
    $outp .= '"SongArtist":"'   . $rs["song_artist"]        . '",';
    $outp .= '"SongAlbum":"'. $rs["song_album"]     . '"}'; 
}
$outp ='{"records":['.$outp.']}';
$conn->close();

echo($outp);
?>

1 个答案:

答案 0 :(得分:1)

您必须访问回复中的data对象(response.data.records):

$http.get("http://rhys-bennett.uk/AIDc3391265/includes/formatjson.php")
 .then(function (response) {
    $scope.names = response.data.records;
 });