Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用angularjs从表中删除行_Javascript_Angularjs - Fatal编程技术网

Javascript 如何使用angularjs从表中删除行

Javascript 如何使用angularjs从表中删除行,javascript,angularjs,Javascript,Angularjs,我想使用angularjs从表中删除该行。但是它工作不正常,它删除了前一行。不是正确的行。如何纠正这一点。 请看工作表 代码截取 <body ng-app="myApp" ng-controller="tasksCtrl"> <div> <table class="table table-striped"> <tbody> <tr ng-repeat="task

我想使用angularjs从表中删除该行。但是它工作不正常,它删除了前一行。不是正确的行。如何纠正这一点。 请看工作表

代码截取

<body ng-app="myApp" ng-controller="tasksCtrl">
    <div>
        <table class="table table-striped">
            <tbody>
                <tr ng-repeat="task in tasks">
                    <td>
                        <a class="btn" data-toggle="modal" data-ng-click="removeRow(task)">Delete</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        var app = angular.module('myApp', []);

        app.controller('tasksCtrl', function($scope, $http) {
            $http.get("data.json")
                //$http.get("/todo/api/v1.0/tasks")
                .success(function(response) {
                    console.log(response.tasks)
                    $scope.tasks = response.tasks;
                });

            $scope.removeRow = function(task) {
                $scope.tasks.splice(task, 1);
            };
        });
    </script>
</body>
像这样试试

看法

像这样试试

看法


您需要获取尝试删除的索引

为此,

 $scope.removeRow = function(task) {
     // get the index
     var index = $scope.tasks.indexOf(task);

     //delete the element from the array
     $scope.tasks.splice(index, 1);
 };
PS:如果您将$index作为数据ng click=removeRow$index通过ng click传递,则只有在ng中没有order by选项时,此操作才会起作用。如果您有排序选项,则删除操作会出错。因为排序时,$index与0,1,2,3相同,但数组顺序可能已更改,例如:0-index元素可以位于已排序数组的1-index中,因此如果传递$index,它将删除实际数组的第一个索引,因此$index不代表实际数组元素

排序选项=>


您需要获取尝试删除的索引

为此,

 $scope.removeRow = function(task) {
     // get the index
     var index = $scope.tasks.indexOf(task);

     //delete the element from the array
     $scope.tasks.splice(index, 1);
 };
PS:如果您将$index作为数据ng click=removeRow$index通过ng click传递,则只有在ng中没有order by选项时,此操作才会起作用。如果您有排序选项,则删除操作会出错。因为排序时,$index与0,1,2,3相同,但数组顺序可能已更改,例如:0-index元素可以位于已排序数组的1-index中,因此如果传递$index,它将删除实际数组的第一个索引,因此$index不代表实际数组元素

排序选项=>


如果你发现这个问题是sencefull,请投票it@AnikIslamAbhicould你可以推荐任何关于这个的教程。。我还有一个问题,请看这个问题有悬赏。如果你发现这个问题是sencefull,请投票it@AnikIslamAbhicould你可以推荐任何关于这个的教程。。我还有一个问题,请看这个问题上有悬赏。你能推荐一些关于这个的教程吗。。我还有一个问题,请看这个问题上有悬赏。你能推荐一些关于这个的教程吗。。我还有一个问题,请看这个问题有悬赏
 $scope.removeRow = function(task) {
     // get the index
     var index = $scope.tasks.indexOf(task);

     //delete the element from the array
     $scope.tasks.splice(index, 1);
 };