无法读取属性' splice'未定义?

时间:2016-06-09 13:56:01

标签: php angularjs

即时通过angular和php构建项目,我试图从数据库表中删除一行。我不知道该怎么处理这个错误可以有人帮忙吗?这是我的代码

php for retrieve-get-allCustomers.php

<?php header('Content-Type: text/html; charset=utf-8');
  $con = mysqli_connect('localhost','root','','hamatkin');
  if(!$con){
    die("couldnt connect".mysqli_error);
  }
  $query = "SELECT `customer_id`, `kind_Of_Customer`, `first_name`, `last_name`, `id`, `city` FROM `customers`";
  $result = $con->query($query);
  $r = array();
  if( $result->num_rows>0){
    while($row = $result->fetch_assoc()){
      $r[] = $row;
    }
  }
  $res = json_encode($r);
   echo json_encode($res);
?>

用于检索的HTML代码:

<!DOCTYPE html>
<html >
<head >

</head>
<body>
    <h2>כרטיסי לקוחות</h2>
    <!-- <a href="/hamatkin/index.html#/customerCardDetails">לחץ לפרטי כרטיס לקוח</a> -->

    <div class="search">
       <label>חיפוש:</label>
       <input type="text" ng-model="search_query">
       <label>מיון לפי:</label>
       <select ng-model="order_query">
           <option value="kind_Of_Customer" >סוג לקוח</option>
           <option value="first_name">שם</option>
           <option value="id">ת"ז</option>
       </select>
       <label>מיון הפוך:</label>
       <input type="checkbox" ng-model="reverse_query" value="reverse" >
   </div>

    <div class="table-responsive">
      <table  class="customer-list table table-striped">
        <thead>
          <tr>
            <!-- <th>#</th> -->
            <th class="Column-Header">מספר לקוח</th>
            <th class="Column-Header">סוג לקוח</th>
            <th class="Column-Header">שם פרטי</th>
            <th class="Column-Header">שם משפחה</th>
            <th class="Column-Header">ת.ז</th>
            <th class="Column-Header">עיר</th>
            <!-- <th>כתובת</th> -->
            <!-- <th>מס' טלפון</th> -->
            <!-- <th>מס' טלפון 2</th> -->
            <!-- <th>אימייל</th> -->
            <!-- <th>פקס</th> -->
            <!-- <th>הופנה דרך</th> -->
            <!-- <th>הערות</th> -->
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="x in customers | filter:search_query | orderBy: order_query:reverse_query">
            <!-- <td>{{$index + 1}}</td> -->
            <td>{{ x.customer_id}}</td>
            <td>{{ x.kind_Of_Customer}}</td>
            <td>{{ x.first_name }}</td>
            <td>{{ x.last_name }}</td>
            <td> {{ x.id}} </td>
            <td> {{ x.city}} </td>
            <td><button ng-click="delete(customers.customer_id, $index)">Delete</button><td>
            <!-- <td> {{ x.address}} </td> -->
            <!-- <td> {{ x.phone}} </td> -->
            <!-- <td> {{ x.phone_2}} </td> -->
            <!-- <td> {{ x.email}} </td> -->
            <!-- <td> {{ x.fax}} </td> -->
            <!-- <td> {{ x.referrer}} </td> -->
            <!-- <td> {{ x.comments}} </td> -->
          </tr>
        </tbody>
      </table>
    </div>
</body>
</html>

php - deleteCustomer.php:

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
  $connect=mysqli_connect('localhost', 'root', '', 'hamatkin');

  if(isset($_GET['customer_id'])){
     $id=$_GET['customer_id'];
     $del="DELETE FROM customers WHERE customer_id='".$id."'";
     mysqli_query($connect,$del);
  }
?>

controller - 此控制器获取所有客户表数据并显示它。它工作正常。剩下的就是我试图删除的东西。:

"use strict";

angular.module('dataSystem').controller('customerCardsCtrl', function($scope,$route,$location,$http) {
  $http({method:'GET', url:'api/get-allCustomers.php/'})
      .then(function(response) {
        var arr = JSON.parse(JSON.parse(response.data));
        $scope.customers = arr;
      })

      // This will log you the error code and trace, if there is an error.
  .catch(function(err) {
    console.log('err', err)
  });

  $scope.delete = function(deletingId, index){
      var params = $.param({"customer_id":deletingId})
      $http({
        url: "api/deleteCustomer.php",
        method:"GET",
        data:params

      }).success(function(data){
        $scope.customers = data;
        $scope.customers.splice(index,1);
        console.log(data)
      });

  }

3 个答案:

答案 0 :(得分:0)

仅使用$scope.data.splice(index, 1);

替换data.slice(index, 1);

返回的数据不在$ scope中。

答案 1 :(得分:0)

在函数范围之外初始化$scope.variable = [];,然后

.success(function(data){
        $scope.variable = data;
        $scope.variable.splice(index,1);
      });

答案 2 :(得分:0)

我认为您在成功回调中尝试做的是从视图中删除已删除的客户。

尝试使用。

$scope.customers.splice(index,1);

成功回调