无法从PHP文件和JSON文件中获取数据
timesheet.php
require 'dbconnect.php';
$array_data = array();
$query = "SELECT * FROM timesheet";
$stmt = $conn->prepare($query);
$stmt->execute($array_data);
$result_data = $stmt->fetchAll( PDO::FETCH_ASSOC );
if( !empty($result_data) ) {
header('Content-type: application/json');
$json = json_encode($result_data);
// echo $json;
echo preg_replace("/null/", '""', $json); // replace null to ""
}
timesheet.json
[
{
"id": 1,
"date": "20th August 2016",
"day": "Sat",
"in1": "10:30",
"out1": "02:30",
"in2": "04:00",
"out2": "08:00",
"in3": "",
"out3": "",
"total_hours": "8:00",
"status": "1"
},
{
"id": 2,
"date": "20th August 2016",
"day": "Sat",
"in1": "10:30",
"out1": "02:30",
"in2": "04:00",
"out2": "08:00",
"in3": "",
"out3": "",
"total_hours": "8:00",
"status": "1"
}
...
]
controller.js
.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) {
var data = tableService.data
//Editable
this.tableEdit = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total:data.length, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
})
的script.js
.service('tableService', ['$http', function($http){
this.data = $http.get("data/timesheet.php")
//this.data = $http.get("data/timesheet.json")
}])
HTML
<table ng-table="tctrl.tableEdit" class="table table-striped table-vmiddle">
<tr ng-repeat="w in $data" ng-class="{ 'active': w.$edit }">
<td data-title="'ID'">
<span ng-if="!w.$edit">{{ w.id }}</span>
<div ng-if="w.$edit"><input class="form-control" type="text" ng-model="w.id" /></div>
</td>
...
</tr>
</table>
当我将json数据放入service.js时,如this.data = [{..}]它正在工作,但现在我需要从exterrnal文件获取数据,该文件可以是JSON文件或PHP文件,但它不能正常工作我正在空桌子。谁能帮帮我吗?我是AngularJs的新手
答案 0 :(得分:0)
尝试使用成功和错误函数作为回调:
http://www.w3schools.com/angular/angular_http.asp
在该页面的底部有一个JSON响应示例。首先,如果您在浏览器中为该php文件发出请求,可以将其放在问题中(查看输出)