2012-08-31 116 views
0

我想实现删除使用PHP angularjs功能如下: 删除: index.html的观点:angularjs删除通过PHP

<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 "; 
?> 

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

+0

我很困惑捕捉它的工作:你为什么把AJAX操作的结果'$ scope.rs'和'$ scope.data',但是不要以任何方式更改'$ scope.result'? – raina77ow

+0

我是一个完整的noob angularjs所以我不知道这个。我应该如何更改$ scope.result?实际上结果是什么? – z22

+0

http://tech-blog.maddyzone.com/javascript/perform-addeditdeleteview-php-using-angular-js希望它会有帮助 –

回答

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; 
     }); 
    }; 
+0

我试过了,但仍然一样。 btw什么是美元指数?它是由angularjs提供的吗?我试图作为$ scope.products.splice(即。$ index,1);然后$ scope.products = data; – z22

+0

当你使用'ng-repeat'的时候,它为它通过'$ index'属性访问的每个项目创建一个数组索引。 – powerc9000

+0

我试过了,还没有工作。该页面不刷新。 – z22