【问题标题】:Data not showing with angular $http service by using php?使用 php 的角度 $http 服务未显示数据?
【发布时间】:2016-05-20 09:40:13
【问题描述】:

我正在用 Angular 构建一个项目,我有一个 html 表单,可以将数据发送到 MYSQL 中的“客户”表。现在我想显示“客户”表的数据。这是我试图获取该数据但它不起作用的代码。谁能帮忙?

php代码:

<?php
  $con = mysqli_connect('localhost','root','','hamatkin');
  if(!$con){
    die("couldnt connect".mysqli_error);
  }
  $query = "SELECT * FROM customers";
  $result = $con->query($query);
  $r = array();
  if( $result->num_rows>0){
    while($row = $result->fetch_assoc()){
      $r[] = $row;
    }
  }
  $res = json_encode($r);
  echo $res;
  echo $sql_statement;
  ?>

控制器:

"use strict";
var app = angular.module('dataSystem',[]);
app.controller('customerCardsCtrl',function($scope,$route,$location,$http){
    $http({method:'GET', url:'api/get-allCustomers.php'}).success(function(response){
        $scope.customers = response.data;
      });
    });
});

html代码:

<!DOCTYPE html>
<html >
<head>
    <title></title>
</head>
<body>


    <div>
      <table ng-controller="customerCardsCtrl" >
        <tr ng-repeat="x in customers">
        <td> {{ x.customer_id}} </td>
        <td>{{ x.first_name }}</td>
        <td>{{ x.last_name }}</td>
        <td> {{ x.id}} </td>
        <td> {{ x.city}} </td>
        <td> {{ x.adress}} </td>
        <td> {{ x.phone}} </td>
        <td> {{ x.email}} </td>
        <td> {{ x.fax}} </td>
        <td> {{ x.referrer}} </td>
        <td> {{ x.comments}} </td>

        </tr>
        </table>
    </div>
</body>
</html>

【问题讨论】:

  • 你遇到了什么错误?
  • html 页面没有显示数据并且保持不变
  • 你在 php 端打印 2 个 json。请删除不正确的 1。
  • phpMyAdmin 是一个用 PHP 编写的工具 MYSQL 是一个 DBMS
  • 你能说得更具体点吗?

标签: php mysql angularjs


【解决方案1】:

改用此代码

"use strict";
var app = angular.module('dataSystem',[]);
app.controller('customerCardsCtrl',function($scope,$route,$location,$http){
  $http({method:'GET', url:'api/get-allCustomers.php'})
    .then(function(response){
           $scope.customers = response.data;
       }, function(response){
           console.log(response)
       });
});

【讨论】:

  • 控制台向我显示:警告尝试多次加载 angular,并且:script1002:语法错误 customerCardsCtrl.js(8,1),并且:错误:[ng:areq] 参数“customerCardsCtrl”不是一个未定义的函数
  • 你删除了多余的 });你加了?
  • 我必须留下多余的 }):因为没有它,页面将无法正常工作
  • 你的代码的其余部分是好的,除了你必须在你的 php 代码中删除 echo $sql_statement;。请尝试编辑后的控制器代码
  • 我删除了 echo_$sql 语句,我尝试了编辑后的代码,但代码使页面无法工作,这就是为什么我必须做一个额外的 });然后页面运行良好,除了不显示“客户”表的详细信息。但感谢您提供帮助!您可以尝试在其他程序中提供帮助吗?(Skype、teamviewer、chrome 远程桌面)
猜你喜欢
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 2021-04-28
相关资源
最近更新 更多