javascript - AngularJs - $http.get 不工作

标签 javascript php angularjs angular-ui-bootstrap

无法从 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/

相关文章:

javascript - 如何使用 AJAX 调用 innerHTML 中的 PHP 值

php - 无法在 PHP 中使用 ffmpeg exec 找到文件并进行压缩

javascript - JavaScript tic tac toe 游戏中的错误

php: 在 catch() 中重复函数调用 2-3 次

javascript - 向多个 Google 自动完成位置添加额外组件

javascript - qtip2 与 AngularJS 的基本使用

javascript - ng-href 中的 Angularjs 函数

jquery-ui - 如何在 Angular 中从一种 ui-sortable 迁移到另一种 ui-sortable?

javascript - 返回假;在 jquery .each 函数中不起作用

javascript - 在 javascript 中执行无限循环是否只对您的计算机资源征税?