我是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其中產生POST請求的功能
$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;它說undefined變量。請幫忙....
$ scope.ticket [0] .ticketid –
,因爲你的$ scope.ticket是一個數組。所以** $ scope.ticketid = $ scope.ticket [0] .ticketid; **可以工作.. –
我試過$ scope.ticketid = $ scope.ticket [0] .ticketid;但它說未定義的變量。 – viparvez