php - 无法使用 angular.js 和 PHP 在 View 上绑定(bind)数据

标签 php mysql angularjs

我需要使用 Angular.js 在页面上显示数据。首先,我使用 PHP 从数据库中获取所有数据。这里页面上没有显示任何数据。我在浏览器控制台中获取以下检索到的数据。

all data [{"0":"1","id":"1","1":"","name":"","2":"","pass":"","3":"","email":""},{"0":"2","id":"2","1":"subhra","name":"subhra","2":"12345","pass":"12345","3":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b686e79735b7c767a727735787476" rel="noreferrer noopener nofollow">[email protected]</a>","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f1f7e0eac2e5efe3ebeeace1edef" rel="noreferrer noopener nofollow">[email protected]</a>"},{"0":"3","id":"3","1":"rakesh","name":"rakesh","2":"0901209474","pass":"0901209474","3":"maini","email":"maini"},{"0":"4","id":"4","1":"raj","name":"raj","2":"23457","pass":"23457","3":"rai","email":"rai"},{"0":"5","id":"5","1":"Rahul","name":"Rahul","2":"098765","pass":"098765","3":"shinha","email":"shinha"}]  

我在下面解释我的代码。

read.js:

var app=angular.module('Read_data',[]);
app.controller('readController',function($scope){
    $.ajax({
        type:'GET',
        url:"php/read.php",
        success: function(data){
            $scope.data=data;
            console.log('all data',$scope.data);
        }
    })
});

read.php:

<?php
//database settings
$connect = mysqli_connect("localhost", "root", "*******", "AngularTest");
$result = mysqli_query($connect, "select * from users");
$data = array();
while ($row = mysqli_fetch_array($result)) {
  $data[] = $row;
}
    print json_encode($data);
?>

index.html:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="Read_data">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo of Angular.js</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/angularjs.js" type="text/javascript"></script>
<script src="js/read.js" type="text/javascript"></script>
</head>

<body>
<center ng-controller="readController">

<div id="body">
 <div id="content">
    <table align="center">
    <tr>
    <th colspan="5"><a href="add_data.html">add data here.</a></th>
    </tr>
    <th> Name</th>
    <th> Email</th>
    <th> Password</th>
    <th colspan="2">Operations</th>
    </tr>
        <tr ng-repeat="users in data">
        <td>{{users.name}}</td>
        <td>{{users.email}}</td>
        <td>{{users.pass}}</td>
  <td align="center"><a href="edit_data.php?edt_id={{users.id}}"><img src="images/pencil_small.png" align="EDIT" /></a></td>
        <td align="center"><a href="javascript:delete_id('')"><img src="images/cross-small-icon.png" align="DELETE" /></a></td>
        </tr>
    </table>
    </div>
</div>

</center>
</body>
</html>

请帮我解决这个问题并成功绑定(bind)表中的所有数据。

最佳答案

Angular 不知道更新的数据。如果您不使用$http,请使用$scope.$apply用于 AJAX。

var app=angular.module('Read_data',[]);
app.controller('readController',function($scope){
    $.ajax({
        type:'GET',
        url:"php/read.php",
        success: function(data){
            $scope.$apply(function(){
                $scope.data = angular.fromJson(data);
            });
            console.log('all data',$scope.data);
        }
    })
});

或使用$http(推荐)

var app=angular.module('Read_data',[]);
app.controller('readController',function($scope, $http){
    $http.get('php/read.php').
        then(function(data) {
            // this callback will be called asynchronously
            // when the response is available
            $scope.data = data.data;
        }, function(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
});

关于php - 无法使用 angular.js 和 PHP 在 View 上绑定(bind)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32778801/

相关文章:

javascript - 如何从php函数中获取字符串值?

php - 我可以在 PHP 中混合使用 MySQL API 吗?

angularjs - 我应该如何在 AngularJS 项目中使用 Gulp 编译 html 模板

javascript - 使用 AngularJS 和输入 [type=file] 显示文件列表

php - 使用 PHP 变量创建 MySQL 表

javascript - Content-Security-Policy在哪里可以配置?

mysql - 无法在 my.ini 文件/(my.cfn 文件)中将自动提交设置为 0

php - 如何从表中获取最新数据?拉维尔 5.5,MySQL

java - 检测 Galera 集群数据库 (mysql) 中的更改。实现应用程序缓存失效

javascript - 输入后有条件地应用 Angular 中的两个类之一?