【问题标题】:How to display data using AngularJS with php如何使用 AngularJS 和 php 显示数据
【发布时间】:2015-10-12 18:49:04
【问题描述】:

我想用 ngconnect.php 连接我的 index.php 页面并想显示我的表格记录。

这是我下面的 index.php 页面......

<html>
        <head>
            <title>first ng app recieve data from database</title>
            <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
        </head>
        <body ng-app="myapp" ng-controller="myctrl">
            <ul>
              <li ng-repeat="x in names">
                {{ x }}
              </li>
            </ul>
            <script>
            $(function(){
                var app = angular.module('myapp',[]);
                app.controller('myctrl',function($scope,$http){
                    $http({method:'GET', url:'ngconnect.php'}).success(function(response){
                        $scope.names = response;
                    });
                });
            });
            </script>   
        </body>
    </html>

这是我下面的 ngconnect.php 页面......

<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysql_connect($servername, $username, $password);
if (!$conn) {
    die("Connection failed: " . $conn->connect_error);
} else {
    $db_selected = mysql_select_db('dsc');  
    if (!$db_selected) {
        die ('Can\'t use dsc : ' . mysql_error());
    } else {
        $result = mysql_query('SELECT * from user');
        if (!$result) {
            die('Invalid query: ' . mysql_error());
        } else {
            $data = array();
            while ($row = mysql_fetch_array($result)) {          
                $data[] = $row;           
            }
            echo json_encode($data);
        }
    }
}
 mysql_close($conn);
?>

为什么我的数据没有显示?

ngconnect.php 确实显示以下内容.....

[{"0":"1","user_id":"1","1":"Amit","first_name":"Amit","2":"","middle_name":"","3":"Kushwaha","last_name":"Kushwaha","4":"akushwaha.softvisionindia@gmail.com","email":"akushwaha.softvisionindia@gmail.com","5":"2","rate_dsc_2":"2","6":"3","rate_dsc_3":"3","7":"4","rate_token":"4","8":"e10adc3949ba59abbe56e057f20f883e","password":"e10adc3949ba59abbe56e057f20f883e","9":"Enabled","status":"Enabled","10":"8858234949","phone":"8858234949","11":"NULL","forget_link":"NULL","12":"00:00:00","forget_time":"00:00:00"},{"0":"2","user_id":"2","1":"Sandeep","first_name":"Sandeep","2":"","middle_name":"","3":"Tripathi","last_name":"Tripathi","4":"sandeep.softvisionindia@gmail.com","email":"sandeep.softvisionindia@gmail.com","5":"10","rate_dsc_2":"10","6":"20","rate_dsc_3":"20","7":"30","rate_token":"30","8":"e10adc3949ba59abbe56e057f20f883e","password":"e10adc3949ba59abbe56e057f20f883e","9":"Enabled","status":"Enabled","10":"9936882864","phone":"9936882864","11":"","forget_link":"","12":"00:00:00","forget_time":"00:00:00"}]

【问题讨论】:

  • 错误?通知?有什么?
  • 如果你触发了你的 usrl ngconnect.php 那么你得到响应然后请提供它
  • 去掉$(function(){来换行角码
  • ngconnect.php 中没有任何错误或通知,但在我的 index.php 中它显示 {{ x.first_name+' '+x.last_name }}。
  • 问题已编辑 Guyesss 请帮帮我.......

标签: php angularjs


【解决方案1】:

试试这些代码..希望对你有帮助..

我假设您的回复是 json 格式(对象数组),因为您发布了您对 php 文件的回复..

<html>
<head>
    <title>first ng app recieve data from database</title>
    <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body ng-app="myapp" ng-controller="myctrl">
    <ul>
      <li ng-repeat="x in names">
        {{ x }}
    </li>
</ul>
<script>
    var myapp = angular.module('myapp', []);

    myapp.controller('myctrl', function ($scope, $http) {
        $http.get('ngconnect.php').success(function(data) {
            $scope.names = data;
        });

    });
</script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 2018-06-28
    • 2017-09-25
    相关资源
    最近更新 更多