来自数组的角度显示数据

时间:2017-01-17 13:24:42

标签: php angularjs

我是angularJS的新手,对javascript知之甚少。

我正在尝试使用$ http.post方法从数据库中检索数据。以下是我从.php文件中获得的内容。

 [{
    "ticketid": "1484637895",
    "categoryid": "1",
    "subcategoryid": "2",
    "ciphonenum": "01814149028",
    "calldescription": "The customer wanted to know all the SKU of Bashundhara Baby Diaper and he requested to inform  him through the mail.",
    "ccrreply": "CCR sent him all SKU of Diaper including the M.R.P.",
    "ccraction": "N\/A",
    "output": "N\/A",
    "remarks": "N\/A",
    "contactinfoname": "MD.Masud",
    "ciaddress": "Banani,DOHS\nDhaka."
 }]

这是我的php文件的样子

<?php
$data = json_decode(file_get_contents("php://input"));

require 'db-config.php';

$ticketid = $data->id;

$sql = "SELECT t.ticketid, t.categoryid, t.subcategoryid, td.ciphonenum,     td.calldescription, td.ccrreply,
td.ccraction, td.output, td.remarks, ci.contactinfoname, ci.ciaddress FROM     ticketdetails td 
INNER JOIN tickets t on t.ticketid = td.ticketid
INNER JOIN contactinfodetails ci ON ci.ciphonenum = td.ciphonenum WHERE     t.ticketid = '$ticketid'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
   $data = array();
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
} else {
    echo "0 results";
}
echo json_encode($data);
$conn->close();
?>

以下是生成帖子请求的angularjs中的函数

$scope.showTicket = function() {
        var id = $routeParams.id;
        $http.post('api/showTicket.php',{'id':id}).then(function(response){
            $scope.ticket = response.data;
            console.log($scope.ticket);
        });
    };

我想只显示数组中的ticketid和calldescription,但只要我指定值就像     $ scope.ticketid = $ scope.ticket.ticketid; 它说未定义的变量。请帮帮....

1 个答案:

答案 0 :(得分:0)

您的JSON是对象数组,而不是对象

所以不要执行$scope.ticketid = $scope.ticket.ticketid,请尝试:

$scope.ticketid = $scope.ticket[0].ticketid;

由于您的数组只包含一个对象,因此它可能就是解决方案......