首页 > 解决方案 > AngularJS 我懒得正确缩进或写一个好标题

问题描述

单击戴尔按钮时,ID 已正确传递给 del-function,但 del-function 无法将 id 发送到 del.php 页面。

 <div ng-controller="thecontroller">
         <table class="table">
             <head>
                 <th>Name</th>
                 <th>E-mail</th>
                 <th>Passward</th>
             </head>
             <tbody  ng-repeat="x in list">
                 <td>{{x.name}}</td>
                 <td>{{x.email}}</td>
                 <td>{{x.passward}}</td>
                 <td>
                    <button ng-click="" class="glyphicon glyphicon-plus"></button>
                    <button ng-click="del(x.id)" class="glyphicon glyphicon-remove"></button>
                </td>
                             </tbody>
         </table>
        </div>  



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

app.controller('thecontroller',function($scope,$http){



 $http.get("show.php")
  .then( 
      function(response){
          $scope.list=response.data;

              });

自爆功能无法发送 id.undefine index id 错误发生

$scope.del=function(value){
       console.log(value);         
              $http.post('del.php',{"id":value}
              )
            }   


});

标签: angularjspost

解决方案


请确保id对象列表中存在属性,请参考下面的示例,其中我使用了场景的模型并且del函数正在接收id.

var app = angular.module('myApp', []);
app.controller('thecontroller', function($scope, $http, $timeout) {
  var data = [{
    id:1,
    name: "one",
    email: "one@one.com",
    password: "1234"
  }, {
    id:2,
    name: "two",
    email: "two@two.com",
    password: "1234"
  }, {
    id:3,
    name: "three",
    email: "three@three.com",
    password: "1234"
  }, ]
  $timeout(function() {
    $scope.names = data;
  }, 3000);
  /*$http.get("show.php")
    .then(function(response) {

      $scope.names = response.data;
      console.log(response)
    });*/
    
  $scope.del=function(value){
       console.log(value);  
  }
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>

<body ng-app="myApp">
  <div ng-controller="thecontroller">
    <form>
      <table>
        <tbody>
          <tr ng-repeat="x in names">
            <td>{{x.name}}</td>
            <td>{{x.email}}</td>
            <td>{{x.password}}</td>
            <td>
              <button ng-click="" class="glyphicon glyphicon-plus"></button>
              <button ng-click="del(x.id)" class="glyphicon glyphicon-remove"></button>
            </td>
          </tr>
        </tbody>
      </table>
    </form>
  </div>
</body>

</html>


推荐阅读