dongzouxigu12345 2012-08-31 13:05
浏览 43

angularjs通过php删除

I am trying to implement delete functionality in angularjs using php as follows: delete: index.html view:

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

in controller: delete:

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

and in php file: 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 ";
?>

The record gets deleted but the page is not refreshed. I cant see the record deleted until I refresh the page. Where am I going wrong? how do I refresh the page?

  • 写回答

1条回答 默认 最新

  • dongtun2572 2012-08-31 22:05
    关注

    You need to delete it from your scope for it to show up. We don't need to pass the element Id to the scope to get it to work when we click we can capture it with the this keyword

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

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line