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);
?>
我正在尝试使用Angular显示表但不会显示。 有什么不对吗? 代码如下。 该表在源代码中就在那里,但它内部没有生成任何内容。 我是Angular的新手。 p>
前端 p>
&lt; div ng-app =“musicApp”ng-controller =“ musicCtrl“&gt;
&lt; table&gt;
&lt; tr ng-repeat =”x in names“&gt;
&lt; td&gt; {{x.Name}}&lt; / td&gt;
&lt; td&gt; ; {{x.SongArtist}}&lt; / td&gt;
&lt; td&gt; {{x.SongAlbum}}&lt; / td&gt;
&lt; / tr&gt;
&lt; / table&gt;
&lt; / div&gt;
&lt; script&gt;
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;});
});
&lt; / script&gt;
code> pre>
PHP后端 p>
&lt;?php
header(“Access- Control-Allow-Origin:*“);
header(”Content-Type:application / json; charset = UTF-8“);
$ result = $ conn-&gt; query(”SELECT song_name, song_artist,song_album FROM musictable“);
$ outp =”“;
while($ rs = $ result-&gt; fetch_array(MYSQLI_ASSOC)){
if($ outp!=”“){$ outp。 =“,”;}
$ outp。='{“姓名”:“'。 $ rs [“song_name”]。 '',';
$ outp。='“歌唱家”:“'。 $ rs [“song_artist”]。 '',';
$ outp。='“SongAlbum”:“'。 $ rs [“song_album”]。 ''}';
}
$ outp ='{“记录”:['。$ outp。']}';
$ conn-&gt; close();
echo($ outp);
?&gt;
code> pre>
div>
答
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;
});