从PHP AngularJS获取JSON没有显示在控制器 [英] Getting JSON with AngularJS from PHP is not showing on controller

查看:172
本文介绍了从PHP AngularJS获取JSON没有显示在控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建基于前端到后端PHP基础应用的AngularJS。我建立一个PHP和放大器JSON对象; MySQL查询。该JSON是正确导出,如果我使用内嵌客体与 NG-INIT 指令数据所示。但是,当我安装在控制器东西 Json.php 文件似乎是错误的,因为我没有看到任何数据。

I am trying to build an AngularJS based front-end to a PHP back-end base application. I build the JSON object with a PHP & MySQL query. The JSON is exported correctly and the data is shown if i use the objet inline with an ng-init directive. But when I mount the Json.php file on the controller something seem to be wrong because I don't see any data.

这是中的链接的例子中,在这里,控制器是<$ C $下方C>&LT; /身体GT; 标记。

This is the link to the example, Here the controller is beneath the </body>tag.

这是主要的页

<html ng-app="myApp">
    <head>
        <title></title>
    </head>
    <body>
        <div style="direction: ltr">

        </div>
        <h1>תוצאות שאלון</h1>
        <div class="table-responsive" ng-controller="ContentCtrl">          

        <table class="table" id="results" >
            <thead>
                <tr>
                    <th>id</th>
                    <th>q1</th>
                    <th>q2</th>
                    <th>q3</th>
                    <th>textarea</th>
                    <th>phone</th>
                    <th>name</th>
                </tr>
            </thead>
            <tbody>
<tr ng-repeat="answer in answers.data">

                    <td>{{answer.id}}</td>
                    <td>{{answer.q1}}</td>
                    <td>{{answer.q2}}</td>
                    <td>{{answer.q3}}</td>
                    <td>{{answer.textarea}}</td>
                    <td>{{answer.phone}}</td>
                    <td>{{answer.name}}</td>
</tr>



                <?php/* 
                $table = new Results();
                $table->allrows();
                 */?>
            </tbody>
        </table>
        </div>
       <script src="http://code.jquery.com/jquery.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
        <script type="text/javascript" src="js/js.js"></script>
        <!--<script src="js/controllers.js"></script>-->
        <script type="text/javascript">

这是控制器:

    var myApp = angular.module('myApp', []);

myApp.controller('ContentCtrl', ['$scope', '$http', function ($scope, $http) {
    $http.get('json.php')

    .success(function(data) {

        $scope.answers = data;

        console.log('test');
    });
}]);

也许模块应该另一页上?
我问<一个href=\"http://stackoverflow.com/questions/34941448/php-export-array-to-json-for-an-angular-app/34941966?noredirect=1#comment57616213_34941966\">this问题早。也许它可以帮助弄好了。

Maybe the module should be on another page? I asked this question earlier. Maybe it can help somehow.

推荐答案

这是你的 json.php 文件与响应的JSON只是对象的数组。但是,你的 NG-重复是遍历 answers.data ,这是不确定的。你应该遍历答案因为这是该数组。

The JSON that your json.php file responds with is just an array of objects. But your ng-repeat is iterating over answers.data, which is undefined. You should iterate over answers since that's the array.

<tr ng-repeat="answer in answers">

如果由于某种原因你不想更改 NG-重复,那么你就可以改变你的控制器,以便它包装数据中的对象是这样的:

If for some reason you don't want to change the ng-repeat, then you can change your controller so that it wraps data in an object like this:

$scope.answers = { data: data };

这篇关于从PHP AngularJS获取JSON没有显示在控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆