[英]AngularJs - $http.get is not working
無法從PHP文件中獲取數據。
PHP
require 'dbconnect.php';
$array_data = array();
$query = "SELECT * FROM user";
$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 ""
}
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.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 = [{..}]它正在工作,但現在我只是試圖從php文件中提取數據,這是無法正常工作並獲得空白表。 誰能幫助我,這里缺少什么? 我是AngularJS的新手。
嘗試這個...
.service('tableService', ['$http', function($http){
this.data = $http.get("yourphpfile.php");
}])
然后
.controller('tableCtrl', function ($filter, $sce, ngTableParams, tableService) {
this.tableEdit = {};
tableService.data.then(initNgTable);
function initNgTable(data) {
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()));
}
});
}
})
希望這個能對您有所幫助!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.