PHP 将数组导出为 json 以获得角度应用程序


PHP export array to json for an angular app

我将数据从MySql db导出到Json对象。我想要实现的实际上是通过角度查看前端数据操作。我认为问题出在我的控制器上。我将 Json 对象内联放置在 ng-init 中,效果很好。

我试图依靠这个问题,但没有取得多大成功

这是我的 HTML 文件

    <html ng-app="myApp">
<head>
    <title></title>
</head>
<body>
    <div style="direction: ltr">
    </div>
    <h1>תוצאות שאלון</h1>
    <div class="table-responsive" ng-controller="ContentCtrl">
        <!--ng-controller="ContentCtrl"-->
        <table class="table" id="results">
            <thead>
                <tr>
                    <th>id</th>
                    <th>q 1</th>
                    <th>q 2</th>
                    <th>q 3</th>
                    <th>textarea</th>
                    <th>phone</th>
                    <th>name</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="answerswer in answers">
                    <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>
            </tbody>
        </table>
    </div>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <script src="http://code.jquery.com/jquery.js"></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>
</body>
</html>

这是控制器.js文件

    var myApp = angular.module('myApp', []);
myApp.controller('ContentCtrl', ['$scope', '$http', function ($scope, $http) {
    $http.get('json.php')
    .success(function(data) {
        $scope.answers = data;
    });
}]);

这是我在json.php文件中得到的json,controller.jsjson.php在同一个文件夹中

[{"id":"6","time":"1453381291","answerswers":"x","q1":"2","q2":"3","q3":"1","textarea":"test","phone":"954472","name":"Jane"},{"id":"5","time":"1453364067","answerswers":"x","q1":"1","q2":"2","q3":"3","textarea":"test 1","phone":"954472","name":"John"},{"id":"4","time":"1453363983","answerswers":"x","q1":"4","q2":"3","q3":"2","textarea":"test 2","phone":"954472","name":"David"}]

现在,我在咨询处没有收到任何相关错误,但我也没有看到数据。

任何帮助都会很棒。

更改您包含的 javascript 文件的顺序,如下所示,这将解决您的问题。

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

对于jquery.js文件必须在 boostrap.min.js 之前加载。