$ http.get - 在AngularJS中无法获取数据

时间:2017-06-17 12:39:21

标签: php angularjs

我是AngularJs的绝对初学者,我刚创建了一个简单的角度js应用程序,用于从MySQL数据库中获取详细信息并在HTML页面中显示相同内容。在运行代码时,我没有得到任何响应。下面是我编写的HTML代码,AngularJS脚本和PHP服务器端代码。我的代码有什么问题吗?谢谢你的进步。

  

AngularJS脚本

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<script>
var app = angular.module("myApp", []);
app.controller("usercontroller", function($scope) {
    $http.get("select.php")
    .success(function(data){
      alert("data retrieved");
    });
});

</script>

  

HTML

<form class="cf">
<div ng-app="myApp" ng-controller="usercontroller">
  <div class="half left cf">
    <input type="text" ng-model="firstname" id="input-name" placeholder="Name">
    <input type="email" ng-model="email" id="input-email" placeholder="Email address">
    <input type="text" ng-model="subject" id="input-subject" placeholder="Subject">
  </div>
  <div class="half right cf">
    <textarea name="message" ng-model="message" type="text" id="input-message" placeholder="Message"></textarea>
  </div>  
  <input type="submit" name="btnInsert" value="Add Data" id="input-submit">


  <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Email</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="x in names">
          <td>{{x.name}}</td>
          <td>{{x.email}}</td>
        </tr>
      </tbody>
  </table>


</div>
</form>
  

PHP

<?php 
//database settings
$connect = mysqli_connect("hostname", "root", "", "test");

$result = mysqli_query($connect, "select * from contact");

$data = array();

while ($row = mysqli_fetch_array($result)) {
  $data[] = $row;
}
    print json_encode($data);
 ?>

0 个答案:

没有答案