Getting JSON with AngularJS from PHP is not showing on controller

我的未来我决定 提交于 2019-12-12 05:19:07

问题


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.

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

This is the main page

<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">

this is the controller:

    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');
    });
}]);

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


回答1:


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">

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 };


来源:https://stackoverflow.com/questions/34949130/getting-json-with-angularjs-from-php-is-not-showing-on-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!