angularjs插入页面刷新

时间:2012-09-03 12:00:40

标签: php angularjs page-refresh

我希望通过AngularJS和PHP在MySQL数据库中添加记录。记录添加成功但没有看到AngularJS效果。即页面不会自动刷新。下面是代码:

控制器

 $scope.add = function() {
                var elem = angular.element($element);
                var dt = $(elem).serialize();
                //alert($element);
                console.log($(elem).serialize());
                $http({
                    method: 'POST',
                    url: 'php/add.php',
                    data: dt,
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function(data, status) {
                    $scope.status = status;
                    $scope.data = data;
                    console.log(data);
                    data.push(this);
                    $scope.products = data; // Show result from server in our <pre></pre> element
                }).error(function(data, status) {
                    $scope.data = data || "Request failed";
                    $scope.status = status;
                });
            };

的index.html

<li ng-repeat="product in products" ng-model = 'products'>
                {{product.description}}|{{product.name}} | <a href='edit.html' ng-click = "edit(product.product_id)">edit</a> | <a href='' ng-click = "del(product.product_id)" ng-hide="isHidden">delete</a>
                <input type="hidden" name="hdnid" ng-model="hdn" value="{{product.product_id}}"/>
                </li>

add.php

    <?php
include 'connect.php';
mysql_select_db($database,$con);  
$nm = $_POST['keywords'];
$desc = $_POST['desc'];
$query = "INSERT INTO `product`(`name`,`description`) VALUES ('$nm', '$desc')";
$result = mysql_query($query) OR die(mysql_error()); 
?>

如何自动刷新页面 ?

1 个答案:

答案 0 :(得分:0)

当您分配新数组或修改现有的$ scope.products时,它会自动列出html中新/修改列表中的每个产品。您无需重新加载页面。

在你的情况下,帖子的响应数据应该是json数组,如果不是,它就不会工作。

像mysql_query($ query).toJSON之类的东西应该可以工作。

如果你想刷新页面(出于一些非常的原因),你应该在http调用的成功中明确地这样做。请记住,使用$ http包装器进行的GET / POST调用是ajax调用。

示例:http://jsfiddle.net/Y3b3H/12/