【问题标题】:Using Angular.js to access php array data使用 Angular.js 访问 php 数组数据
【发布时间】:2016-05-06 17:35:25
【问题描述】:

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

有什么建议吗?

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);

        });

【问题讨论】:

    标签: javascript php arrays angularjs


    【解决方案1】:

    您的阵列只会容纳 一个 参与者。在循环上方声明它,并在其中追加:

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

    您的客户端 Angular 代码中存在类似问题:

    // 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);
    

    【讨论】:

    • 感谢您的回复真的很感激它不起作用,我得到了很多都是空的对象。在我的角度中选择正确的值有问题吗?
    • @Ryan 不用担心,很高兴为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 2016-01-14
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多