使用Angular.js访问php数组数据


Using Angular.js to access php array data

我已经问过这样的问题,但我遇到了问题。我使用angular.js将数据发送到php文件。这个php文件正在收集一个数据列表,并将其存储在一个数组中。然后,我对这个数组进行编码,并在成功函数中将其发送回angular。我需要一个接一个地显示每个阵列。

有什么建议吗?

if($result){
 while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

  $Participants = array(
            firstname => $row['firstname'],
            lastname => $row['lastname'],
            amount => $row['longitude']
        );

 }
}
echo json_encode($Participants);

我的角度

     angular.forEach(response.data, function(value, key){
        var Participants = {};
        Participants = {
          firstname: value['firstname'],
          lastname: value['lastname'],
          amount: value['amount'], 
        };
        console.log(Participants);
        });

您的数组将只容纳一个参与者。在循环上方声明它,并在以下范围内附加到它:

$Participants = array();
if($result) {
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        $Participants[] = array(
            firstname => $row['firstname'],
            lastname => $row['lastname'],
            amount => $row['longitude']
        );
}

在您的客户端角度代码中也存在类似的问题:

// Note this is now an array instead of plain object
var Participants = []; 
angular.forEach(response.data, function(value, key){
    Participants.push({
        firstname: value['firstname'],
        lastname: value['lastname'],
        amount: value['amount'], 
    });
});

console.log(Participants);