0

我正在尝试使用 php 在 angularjs 中实现删除功能,如下所示:删除:index.html 视图:

<ul>
            <li ng-repeat="r in result">
            {{r.description}}|{{r.name}} | <a href='' ng-click = "edit(r.product_id)">edit</a> | <a href='' ng-click = "delete(r.product_id)">delete</a>
            <input type="hidden" name="hdnid" ng-model="hdn" value="{{r.product_id}}"/>
            </li>
        </ul>

在控制器中:删除:

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

并在 php 文件中:delete.php

    <?php
include 'connect.php';
mysql_select_db($database,$con);  
$id = $_POST['hdnid'];
$query = "delete from `product` where `product_id` = $id";
$result = mysql_query($query) OR die(mysql_error()); 
echo "You have successfully deleted ";
?>

记录被删除,但页面未刷新。在刷新页面之前,我看不到已删除的记录。我哪里错了?如何刷新页面?

4

1 回答 1

2

您需要将其从范围中删除才能显示。当我们点击时,我们不需要将元素 ID 传递给范围以使其工作,我们可以使用this关键字捕获它

 $scope.delete = function() {
        //Store the this variable so we can use it in the $http functions
        var that = this
        var elem = angular.element($element);
        var dt = $(elem).serialize();
        //alert($element);
        console.log($(elem).serialize());
        $http({
            method: 'POST',
            url: 'php/delete.php',
            data: dt,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(data, status) {
            // get the elements index and then remove it from the results array

            $scope.result.splice(that.$index, 1);
            $scope.status = status;
            $scope.data = data;
            $scope.rs = data; // Show result from server in our <pre></pre> element
        }).error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;
        });
    };
于 2012-08-31T22:05:14.950 回答