PHP出口数组JSON的角度应用 [英] PHP export array to json for an angular app

查看:136
本文介绍了PHP出口数组JSON的角度应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我出口从MySQL数据库的数据以JSON对象。
我想实现实际上是棱角分明看到前端的数据操作。
我认为这个问题是在我的控制器。我把JSON对象内联 NG-INIT 和它的伟大工程。

我试图依靠<一个href=\"http://stackoverflow.com/questions/25064570/get-json-data-from-a-php-file-for-an-angular-scope\">this问题但没有取得多大成功。

这是我的HTML文件

 &LT; HTML NG-应用=对myApp&GT;&LT; HEAD&GT;
    &LT;标题&GT;&LT; /标题&GT;
&LT; /头&GT;&LT;身体GT;
    &LT; D​​IV的风格=方向:LTR&GT;
    &LT; / DIV&GT;
    &LT; H1&GT;תוצאותשאלון&LT; / H1&GT;
    &LT; D​​IV CLASS =表响应NG控制器=ContentCtrl&GT;
        &LT;! - 纳克控制器=ContentCtrl - &GT;
        &LT;表类=表ID =结果&GT;
            &LT;&THEAD GT;
                &所述; TR&GT;
                    &LT;第i个ID&LT; /第i
                    &LT;第i个Q 1&LT; /第i
                    &所述;第i,Q 2'/第i
                    百分位&GT;问第3版/第i
                    &LT;第i textarea的&LT; /第i
                    &LT;第i个电话&LT; /第i
                    &LT;第i名&LT; /第i
                &LT; / TR&GT;
            &LT; / THEAD&GT;
            &LT;&TBODY GT;
                &LT; TR NG重复=答案回答&GT;
                    &所述; TD&GT; {{answer.id}}&下; / TD&GT;
                    &所述; TD&GT; {{answer.q1}}&下; / TD&GT;
                    &所述; TD&GT; {{answer.q2}}&下; / TD&GT;
                    &所述; TD&GT; {{answer.q3}}&下; / TD&GT;
                    &所述; TD&GT; {{answer.textarea}}&下; / TD&GT;
                    &所述; TD&GT; {{answer.phone}}&下; / TD&GT;
                    &所述; TD&GT; {{answer.name}}&下; / TD&GT;
                &LT; / TR&GT;
            &LT; / TBODY&GT;
        &LT; /表&gt;
    &LT; / DIV&GT;
    &LT;脚本SRC =htt​​ps://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js诚信=sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xScrossorigin =匿名&GT;&LT; / SCRIPT&GT;
    &所述; SCRIPT SRC =HTTP://$c$c.jquery.com/jquery.js&GT;&下; /脚本&GT;
    &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js&GT;&下; /脚本&GT;
    &LT;脚本类型=文/ JavaScript的SRC =JS / js.js&GT;&LT; / SCRIPT&GT;
    &LT;脚本SRC =JS / controllers.js&GT;&LT; / SCRIPT&GT;
&LT; /身体GT;&LT; / HTML&GT;

这是controller.js文件

  VAR对myApp = angular.module('对myApp',[]);myApp.controller('ContentCtrl',['$范围,$ HTTP',函数($范围,$ HTTP){
    $ http.get('json.php')
    .success(功能(数据){
        $ scope.answers =数据;
    });
}]);

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

<$p$p><$c$c>[{\"id\":\"6\",\"time\":\"1453381291\",\"answers\":\"x\",\"q1\":\"2\",\"q2\":\"3\",\"q3\":\"1\",\"textarea\":\"test\",\"phone\":\"954472\",\"name\":\"Jane\"},{\"id\":\"5\",\"time\":\"1453364067\",\"answers\":\"x\",\"q1\":\"1\",\"q2\":\"2\",\"q3\":\"3\",\"textarea\":\"test 1\",\"phone\":\"954472\",\"name\":\"John\"},{\"id\":\"4\",\"time\":\"1453363983\",\"answers\":\"x\",\"q1\":\"4\",\"q2\":\"3\",\"q3\":\"2\",\"textarea\":\"test 2,手机:954472,名:大卫}]

现在,我没有得到在consule任何相关的错误,但我没有看到任何数据。

任何帮助将是巨大的。


解决方案

更改已包含如下,JavaScript文件的顺序将解决您的问题。

 &LT;脚本SRC =HTTP://$c$c.jquery.com/jquery.js&GT;&LT; / SCRIPT&GT;
        &LT;脚本SRC =htt​​ps://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js诚信=sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xScrossorigin =匿名&GT;&LT; / SCRIPT&GT;
        &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js&GT;&下; /脚本&GT;
        &LT;脚本类型=文/ JavaScript的SRC =JS / js.js&GT;&LT; / SCRIPT&GT;
&LT;脚本SRC =JS / controllers.js&GT;&LT; / SCRIPT&GT;

有关的jquery.js 的boostrap.min.js之前文件必须加载。

I exported data from MySql db to Json object. What I want to achieve is actually to see the front end data manipulations by angular. I think that the problem is in my controller. I placed the Json object inline in ng-initand it works great.

I tried to lean on this question but with not much success

This is my HTML file

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

this is the controller.js file

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

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

This is my the json i get at the json.php file, controller.js and json.php are in the same folder

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

Now, I do not get any related error at the consule but I do not see data either.

any help would be great.

解决方案

Change the order of the javascript files you have included as below this will resolve your problem.

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

For jquery.js file must load before the boostrap.min.js.

这篇关于PHP出口数组JSON的角度应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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