在 Angular ngrepeat 中使用从服务器返回的数据


using the data returned from server in angular ngrepeat

我一直在尝试做的是使用 angular 的服务从服务器获取数据$http.js然后使用 ng-repeat 指令在我的模板中使用返回的数据。但问题是获取的数据根本没有显示,并且 ng-repeat 指令像数字行一样生成,除了它应该显示的那个。我正在使用PHP来渲染数据。这是javascript部分:

    function display($scope, $http){
      $scope.s= "?s=Spice";
      $http({method: "GET", url: "getlistangular.php"+$scope.s}).
       success(function(data){
        alert("success");
        $scope.list= data;
       }).
       error(function(){
        alert("failed");
           });
     }

这是 php 中的脚本(spname、spprice、imgsrc 是 mysql 'spice' 表中的列名):

    $t = $_GET['s'];
    $query= mysql_query("select * from $t");
    echo "[";
    while ($row = mysql_fetch_array($query))
    {
    echo "{img:'".$row[imgsrc]."', name:'".$row[spname]."', price:'".$row[spprice]."'},'n";
    }
    echo "];";

这是模板部分:

    <table ng-controller="display">
                <tr>
                    <th> </th> <th> Name </th> <th> Price (Rs.) </th>
                </tr>
                <tr ng-repeat="con in list">
                    <td><input type="checkbox" class="regular-checkbox" value={{con.name}}><img src="{{con.img}}"/></td>
                    <td>{{con.name}}</td>
                    <td>{{con.price}}</td>
                </tr>
            </table>

我是这个棱角分明.js的东西的新手,所以如果你觉得这是一个愚蠢的问题,我为此道歉。

提前感谢!

Javascript 应该是这样的:

var app = angular.module('myApp', []);
app.controller('displayCtrl', function($scope, $http) {
   $scope.s= "?s=Spice";
  $http({method: "GET", url: "getlistangular.php"+$scope.s, isArray: true}) //change here
    .success(function(response){
       alert("success");
       $scope.list= response; //change here
    })
    .error(function(){
       alert("failed");
    });
 });

网页部分 :

<table ng-controller="display">

替换为 :

<table  ng-controller="displayCtrl">

 <html>

替换为

    <html ng-app="myApp">

在此处查看演示

问题基本上出在 php 部分。解决方案是以键:值格式返回一个 JSON 数组,而不是使用 echo 创建它:

    echo "[";
while ($row = mysql_fetch_array($query))
{
echo "{img:'".$row[imgsrc]."', name:'".$row[spname]."', price:'".$row[spprice]."'},'n";
}
echo "];";

应替换为:

    $arr = array();
while ($row = mysql_fetch_array($query))
{
$arr[] = array( 'img' => $row['imgsrc'], 'name' => $row['spname'], 'price' => $row['spprice'] );
}
echo json_encode(array('data' => $arr));