通过php删除angularjs

通过php删除angularjs,php,angularjs,sql-delete,page-refresh,Php,Angularjs,Sql Delete,Page Refresh,我尝试使用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(

我尝试使用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>
  • {{r.description}}}{{r.name}}}
在控制器中: 删除:

//删除
$scope.delete=函数(){
var elem=角度元素($element);
var dt=$(elem).serialize();
//警报($要素);
log($(elem.serialize());
$http({
方法:“POST”,
url:'php/delete.php',
数据:dt,
标题:{'Content-Type':'application/x-www-form-urlencoded'}
}).成功(功能(数据、状态){
$scope.status=状态;
$scope.data=数据;
$scope.rs=data;//在元素中显示来自服务器的结果
    <?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 ";
?>
}).错误(功能(数据、状态){ $scope.data=data | |“请求失败”; $scope.status=状态; }); };
在php文件中:delete.php

 $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;
        });
    };

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

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

$scope.delete=函数(){
//存储这个变量,以便我们可以在$http函数中使用它
var=this
var elem=角度元素($element);
var dt=$(elem).serialize();
//警报($要素);
log($(elem.serialize());
$http({
方法:“POST”,
url:'php/delete.php',
数据:dt,
标题:{'Content-Type':'application/x-www-form-urlencoded'}
}).成功(功能(数据、状态){
//获取元素索引,然后将其从结果数组中删除
$scope.result.splice(即$index,1);
$scope.status=状态;
$scope.data=数据;
$scope.rs=data;//在元素中显示来自服务器的结果
    <?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 ";
?>
}).错误(功能(数据、状态){ $scope.data=data | |“请求失败”; $scope.status=状态; }); };
我很困惑:为什么您要将AJAX操作的结果分配给
$scope.rs
$scope.data
,但却不以任何方式更改
$scope.result
?我完全不了解angularjs,所以我不知道这一点。我应该如何更改$scope.result?结果究竟是什么?我试过了,但还是一样。顺便问一下,什么是$index?它是由angularjs提供的吗?我尝试将其作为$scope.products.splice(即$index,1);然后$scope.products=数据;当您使用
ng repeat
时,它会为它循环的每个项目创建一个数组索引,以便通过items
$index
属性访问。看看我制作的这把小提琴,看看您是否可以在那里使用任何东西。