gpt4 book ai didi

javascript - AngularJs - $http.get 不工作

转载 作者:行者123 更新时间:2023-11-28 05:28:05 25 4
gpt4 key购买 nike

无法从 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()));
}
});
})

脚本.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 数据像 this.data = [{..}] 一样放入 service.js 中时,它正在工作,但现在我只是尝试从不工作的 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()));
}
});
}
})

希望这对你有帮助!!

关于javascript - AngularJs - $http.get 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com