如何使用angularjs从字符串转换为数组?

时间:2016-07-25 08:13:27

标签: php angularjs

我尝试使用php和angular从数据库表中删除一行。我正在使用" Splice"但我的数据是一个字符串,我需要转换为数组。有人可以帮忙吗?

这是我的代码:

控制器:

  $scope.delete = function(deletingId, $index) {
        var params = $.param({"customer_id":deletingId});
        $http({
          headers: {'Content-Type': 'application/x-www-form-urlencoded'},
          url: 'http://localhost:8081/hamatkin/api/delete-customer.php/',
          method: "GET",
          data: params
        }).success(function(data){
          console.log(data);
          var arr=JSON.parse(JSON.stringify(data))
          $scope.customerDetails = arr;
          $scope.customerDetails.splice($index, 1);

我得到的错误:

angular.js:13550 TypeError: $scope.customerDetails.splice is not a function

删除-customer.php

<?php header('Content-Type: text/html; charset=utf-8');
$connect=mysqli_connect("localhost", "root", "", "hamatkin");

  include_once 'Customer.php';
mysqli_query($connect,"SET character_set_client = utf8");
mysqli_query($connect,"SET character_set_connection = utf8");
mysqli_query($connect,"SET character_set_results = utf8");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$customer = new Customer();


if(isset($_GET['customer_id'])){
   $customer_id = $_GET['customer_id'];
   $del = "DELETE FROM posts WHERE customer_id='".$customer_id."'";
   mysql_query($connect, $del);
 }


$newURL = "/hamatkin/#/customerCards";
header('Location: '.$newURL);
?>

HTML

  <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.full_name}}</td>
            <td><a href="/hamatkin/index.html#/customerCardDetails/{{ x.customer_id}}"  class="btn btn-primary btn- active">הצג פרטי לקוח </a></td>
            <td><a ng-click="delete(x.customer_id, $index)" class="btn btn-primary btn- active">מחיקה</td>
          </tr>
        </tbody>

的console.log(数据)

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Styles -->

        <link rel="stylesheet" href="lib\bootstrap.css" />
        <link rel="stylesheet" href="styles/layout.css" />

        <!-- Liberaries -->
        <script src="scripts/angular.min.js"></script>
        <script src="lib/jquery-1.12.0.min.js"></script>
        <!-- <script src="lib/angular.js"></script> -->
        <script src="lib/angular-route.js"></script>
        <script src="scripts/angular-ui/ui-bootstrap-tpls.min.js"></script>
        <!-- <script src="scripts/angular-route.min.js"></script> -->
        <script src="lib/bootstrap.js"></script>
        <link href="scripts\bootstrap.min.js" />

        <!-- Application Model, Direectives, Services & Controllers -->
        <script src="scripts/App.js"></script>
        <!-- <script src="scripts/angular.min.js"></script> -->

        <!-- controllers-->
        <script src="Controllers/loginCtrl.js"></script>
        <script src="Controllers/customerFormCtrl.js"></script>
        <script src="Controllers/customerCardsCtrl.js"></script>
        <script src="Controllers/stockCtrl.js"></script>
        <script src="Controllers/bodyCtrl.js"></script>
        <script src="Controllers/ordersCtrl.js"></script>
        <script src="Controllers/reportsCtrl.js"></script>
        <script src="Controllers/recordExpensesCtrl.js"></script>
        <script src="Controllers/supplierListCtrl.js"></script>
        <script src="Controllers/supplierFormCtrl.js"></script>
        <script src="Controllers/customerCardDetailsCtrl.js"></script>


        <title>מערכת מידע רומן המתקין</title>
    </head>


    <body ng-app="dataSystem" ng-controller="bodyCtrl">
        <!--Navigation Bar-->
        <nav class="navbar navbar-inverse navbar-fixed-top" ng-hide="isNavTabsHidden()">
            <div class="container-fluid">
                <div class="navbar-header navbar-right">
                    <img class="img-rounded" src="images/black logo.jpg" style="width:155px" />
                </div>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="/hamatkin/#/reports">דוחות</a></li>
                    <li><a href="/hamatkin/#/recordExpenses">הכנסות והוצאות</a></li>
                    <li><a href="/hamatkin/#/stock">ניהול מלאי וספקים</a></li>
                    <li><a href="/hamatkin/#/orders">ניהול הזמנות</a></li>
                    <li><a href="/hamatkin/#/customerForm">ניהול לקוחות</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-left">
                    <li><a href="/hamatkin/#"><span class="glyphicon glyphicon-log-out"></span> התנתק</a></li>
                    <li><a href="#"><span class="glyphicon glyphicon-wrench"></span> ניהול הרשאות</a></li>
                </ul>
            </div>
        </nav>

        <br /><br /><br />


        <!--טאב ניהול לקוחות-->
        <div class="containers-wrapper">
            <div class="container" ng-hide="costumerHidden()">

                    <div class="list-group">
                        <a href="/hamatkin/index.html#/customerForm" class="list-group-item">טופס רישום לקוח</a>
                        <a href="/hamatkin/index.html#/customerCards" class="list-group-item">כרטיסי לקוחות</a>
                        <a href="#" class="list-group-item">רשימת פניות מלקוחות</a>
                    </div>

            </div>

        <!--טאב ניהול מלאי וספקים-->
            <div class="container" ng-hide="stockHidden()">

                    <div class="list-group">
                        <a href="/hamatkin/index.html#/" class="list-group-item">טופס רישום מלאי</a>
                        <a href="/hamatkin/index.html#/" class="list-group-item">טופס עריכת/מחיקת מלאי</a>
                        <a href="/hamatkin/index.html#/" class="list-group-item">רשימת מלאים למכירה</a>
                        <a href="/hamatkin/index.html#/" class="list-group-item">רשימת ציוד עבודה</a>
                        <a href="/hamatkin/index.html#/" class="list-group-item">בדיקת זמינות מלאי</a>
                        <a href="/hamatkin/index.html#/supplierForm" class="list-group-item">טופס רישום ספק</a>
                        <a href="/hamatkin/index.html#/supplierList" class="list-group-item">רשימת ספקים</a>
                    </div>

            </div>

        <!--טאב ניהול הזמנות-->

            <div class="container" ng-hide="ordersHidden()">

                    <div class="list-group">
                        <a href="/hamatkin/index.html#/" class="list-group-item">טופס הזמנה חדשה</a>
                        <a href="/hamatkin/index.html#/" class="list-group-item">רשימת הזמנות</a>
                        <a href="/hamatkin/index.html#/" class="list-group-item">טופס הצעת מחיר</a>
                        <a href="/hamatkin/index.html#/" class="list-group-item">הצעות מחיר בהמתנה</a>
                    </div>

            </div>



        <!--טאב דוחות-->
        <div class="container" ng-hide="reportsHidden()">


                <div class="list-group">
                    <a href="/hamatkin/index.html#/" class="list-group-item">דו"ח מלאים לפי סוגים</a>
                    <a href="/hamatkin/index.html#/" class="list-group-item">דו"ח הצעות מחיר</a>
                    <a href="/hamatkin/index.html#/" class="list-group-item">דו"ח מכירות על פי סוגי מלאי</a>
                    <a href="/hamatkin/index.html#/" class="list-group-item">דו"ח הוצאות לספקים</a>
                    <a href="/hamatkin/index.html#/" class="list-group-item">דו"ח הוצאות לפי חודשים</a>
                    <a href="/hamatkin/index.html#/" class="list-group-item">דו"ח הכנסות לפי חודשים</a>
                    <a href="/hamatkin/index.html#/" class="list-group-item">דו"ח לקוחות חדשים חודשי</a>
                </div>


        </div>

        <!--טאב הכנסות והוצאות-->
        <div class="container" ng-hide="isIncome_expensesHidden()">
          <div class="list-group">
              <a href="/hamatkin/index.html#/" class="list-group-item">טופס תיעוד הוצאה</a>
              <a href="/hamatkin/index.html#/" class="list-group-item">טופס תיעוד הכנסה</a>
              <a href="/hamatkin/index.html#/" class="list-group-item">רשימת הוצאות</a>
          </div>
        </div>


        <!--left side screen-->
        <div class="view-content left-side-screen">
          <ng-view></ng-view>
        </div>
      </div>

        <!--Footer-->
        <div class="footer" ng-hide="isFooterHidden()">

            <p>Copyright @ 2016 Denis & Denis. All Rights Reserved</p>

        </div>

    </body>

</html>

2 个答案:

答案 0 :(得分:0)

你必须首先找到你想要拼接的索引然后你必须拼接, SPlice用于数组项目检查Item是否是数组 for refrence https://plnkr.co/edit/Nzz4T4AmkLmcr7frxyH1?p=preview < / p>

<强> JS

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
    $scope.arrylist = [{
        "id": 53,
        "name": "abced"

    }, {
        "id": 53,
        "name": "gayu"

    }]

    $scope.doc = {
        "id": 53,
        "name": "abced"

    }
    console.log($scope.arrylist)

    var index = $scope.arrylist.findIndex(function(item) {
        if (item.id == $scope.doc.id) {
            return true;
        }
    });

    if (index !== -1) {
        //Removing the test from list;
        $scope.arrylist.splice(index, 1);
    }

    console.log($scope.arrylist)
});

答案 1 :(得分:0)

使用JSON.parse会将JSON转换为JS对象。在您的场景中,您正在调用存在于Array原型上的splice函数,而您的arr变量实际上是一个没有此类函数的纯字符串对象。

假设您的字符串以某种方式格式化,请说 - 客户名称用逗号分隔,您可以使用split函数。这会将您的字符串转换为由所需格式划分的分隔元素数组。

像这样:

var clientsToDelete = arr.split(","); // comma splitting
clientsToDelete.splice($index, 1);