drh96824
drh96824
2016-12-06 14:36
浏览 43
已采纳

Angular SQL - 缺少什么?

I am trying to display a table using Angular but it won't display. Is something wrong/missing? Code below. The table is in the source code as being there, but nothing is generated inside it. I'm new to Angular.

Front-end

    <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 Back-end

    <?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条回答 默认 最新

  • duandeng1824
    duandeng1824 2016-12-06 14:42
    已采纳

    you have to access the data object in your response (response.data.records):

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

相关推荐