如何删除ng-click所在的行?

时间:2015-01-07 11:43:19

标签: angularjs angularjs-ng-click

在以下代码中,当我删除客户时,我希望TR行消失。

最好的方法是什么?我是否需要以某种方式将行作为参数发送到deleteCustomer?我是否可以以某种方式访问​​AngularJS中的TR DOM元素?

<html ng-app="mainModule">
    <head>
        <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
        <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>    
    <body ng-controller="mainController" style="padding: 20px">
        <div class="col-lg-5">
            <table style="width: 500px" class="table-striped table-bordered table table-hover">
                <tr ng-repeat="customer in customers">
                    <td style="width:50px"><button ng-click="deleteCustomer(customer)">Delete</button></td>
                    <td style="text-align:right">{{customer.id}}</td>
                    <td>{{customer.firstName}}</td>
                    <td>{{customer.lastName}}</td>
                </tr>
            </table>
        </div>
        <div class="col-lg-7">
            <div class="panel panel-info">
                <div class="panel-heading">Logger</div>
                <div class="panel-body" style="padding:0">
                    <table class="table table-bordered" style="margin:0">
                        <tr ng-repeat="loggedAction in loggedActions">
                            <td>{{loggedAction.action}}</td>
                            <td>{{loggedAction.description}}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <script>
        var mainModule = angular.module('mainModule', []);
        function mainController($scope) {

            $scope.loggedActions = [];

            $scope.customers = [
                {id: 1, firstName: 'Joe', lastName: 'Thompson'},
                {id: 2, firstName: 'Hank', lastName: 'Howards'},
                {id: 3, firstName: 'Zoe', lastName: 'Frappe'}
            ];

            $scope.deleteCustomer = function (customer) {
                $scope.$emit('customerDeleted', customer);
            };

            $scope.$on('customerDeleted', function (event, customer) {
                $scope.loggedActions.push({action: 'delete', description: 'Deleted customer ' + customer.firstName + ' ' + customer.lastName});
            });
        }
        </script>
    </body>
</html>

2 个答案:

答案 0 :(得分:8)

修改

正如@ K.Toress的评论所指出的,最好通过函数内的indexOf()检索已删除客户的索引,而不是从ng-repeat传递$ index 。 如果使用过滤器或对数组进行排序,传递$ index将会产生意外结果。

deleteCustomer功能:

$scope.deleteCustomer = function (customer) {
  var index = $scope.customers.indexOf(customer);
  $scope.customers.splice(index, 1);
  $scope.$emit('customerDeleted', customer); 
};

已更新 plnkr


你可以使用ng-repeat提供的$ index和delete函数中的array.splice:

HTML:

<button ng-click="deleteCustomer($index, customer)">Delete</button>

JS:

$scope.deleteCustomer = function ($index, customer) {
  $scope.customers.splice($index, 1);
  $scope.$emit('customerDeleted', customer);
};

plnkr

答案 1 :(得分:1)

工作示例:

http://plnkr.co/edit/7MOdokoohX0mv9uSWuAF?p=preview

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

app.controller('MainCtrl', function($scope) {
  $scope.data = ['a', 'b', 'c', 'd', 'e'];
  $scope.delete = function(at) {
    $scope.data.splice(at, 1);
  }
});

模板:

  <body ng-app="plunker" ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    <div ng-repeat="elem in data">
        {{elem}}
        <button ng-click="delete($index)">delete {{elem}}</button>
    </div>

  </body>