页面加载时AngularJs $ http发布

时间:2015-01-19 13:27:28

标签: javascript php angularjs

我的页面是从数据库显示用户。当表单提交时,我可以显示$ http.post数据。但是没有显示任何页面加载。我怎么能显示我的$ http.post数据页面加载(对不起大家我的英语非常糟糕)

这是我的view.html

    <!DOCTYPE html>

<html ng-app="myapp">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src='angular.js' type="text/javascript"></script>
        <script>
            angular.module('myapp', [])
                    .controller('mycon', ['$scope', '$http', function($scope, $http) {

                            var update = function() {
                                $http.post('query.php').success(function(data, status) {
                                    return data;
                                });
                            };

                            $scope.send = function() {
                                var data = JSON.stringify($scope.user);

                                $http.post('query.php', data).success(function(data, status) {
                                    $scope.da = data;
                                    $scope.angularList = data; // or update();
                                });
                            };
                            $scope.angularList = update();

                        }]);
        </script>
    </head>
    <body ng-controller="mycon">

        <ul ng-repeat="mylist in angularList">
            <li>{{mylist.name}}</li>
            <li>{{mylist.password}}</li>
            <li>{{mylist.role}}</li>
        </ul>

        <h1> Add new user:</h1>
        <form ng-submit="send()">
            Name:   <input type="text" ng-model="user.name"/> 
            Login:  <input type="text" ng-model="user.pass"/>

            <select  ng-model="user.role">
                <option>user</option>
                <option>admin</option>
            </select>
            <input type="submit"  value="Add"/>
        </form>
        data: {{da}}<br>
        status: {{st}}
        <hr>
    </body>
</html>

这是我的query.php

 $con = mysql_connect('localhost', 'root', '');
mysql_select_db('phpang');


if(json_decode(file_get_contents("php://input"))){
$data=json_decode(file_get_contents("php://input"));
$fn=$data->name;
$ln=$data->pass;
$rl=$data->role;                                                                                                                         

$query_ins='insert into users (name,password,role) values("'.$fn.'","'.$ln.'","'.$rl.'")';
mysql_query($query_ins);
}
$query='select * from users'; 
$select=mysql_query($query);
$myphplist=[];
while ($row=mysql_fetch_array($select)) {
    $myphplist[]=$row;
}

$myJson=json_encode($myphplist);

echo $myJson;

1 个答案:

答案 0 :(得分:2)

$http进行异步调用,因此您应该使用回调等。

                        var update = function(callback) {
                            $http.get('query.php').success(function(data, status) {
                                callback(data);
                            });
                        };

                        $scope.send = function() {
                            var data = JSON.stringify($scope.user);

                            $http.post('query.php', data).success(function(data, status) {
                                $scope.da = data;
                                $scope.angularList = data; // or update();
                            });
                        };

                        update(function(data){
                            $scope.angularList = data;
                        });

一些提示:

您不需要JSON.stringify($scope.user),您可以传递您的对象,例如$http.post('query.php', $scope.user)

在这种情况下,请使用GET代替POST。仅在对您的数据进行一些更改时才使用POST