Angular CRUD 从模式中删除行

Angular CRUD delete row from modal

我在 MEAN STACK 中使用带有删除功能的简单 CRUD API

 app.delete('/api/users/:user_id', function(req, res) {
    users.remove({
        _id : req.params.user_id
    }, function(err, user) {
        if (err)
            res.send(err);
        users.find(function(err, users) {
            if (err)
                res.send(err)
            res.json(users);
        });
    });
});

控制器

var app = angular.module('usersList', []);
app.controller('usersController', function($scope, $http) {

$http.get('/api/users')
    .success(function(userData) {
        $scope.users = userData;
        $scope.length = userData.length;
    })
    .error(function(data) {
        console.log('Error: ' + data);
    });

$scope.deleteUser = function(id) {
    $http.delete('/api/users/' + id)
        .success(function(data) {
            $scope.users = data;
            console.log(data);
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};

});

在 HTML 文件中,我填充了一个 table,如下所示,通过获取 {{$index}}

 <body data-ng-controller="usersController">            
        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>ID</th>
                    <th>Login</th>
                    <th>Email</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <tr data-ng-repeat="userData in users" >
                    <td><input type="checkbox"/></td>
                    <td>{{ userData._id }}</td>
                    <td>{{ userData.id_userLogin }}</td>
                    <td>{{ userData.email }}</td>
                    <td>                            
                        <!-- Button trigger for Delete modal -->
                        <button type="button" data-toggle="modal" data-target="#deleteModal{{$index}}" data-ng-click="Clear()">
                        <span class="glyphicon glyphicon-trash"></span>
                        </button>
                        <!-- Delete Modal -->
                        <div class="modal fade" id="deleteModal{{$index}}" tabindex="-1" role="dialog">
                          <div class="modal-dialog">
                            <div class="modal-content">
                              <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="myModalLabel">Delete <strong>{{ userData.id_userLogin }}</strong> account</h4>
                              </div>
                              <div class="modal-body">
                                <div class="alert alert-danger" role="alert">Are you sure you want to delete this account?</div>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-danger" data-ng-click="deleteUser(user._id)">Delete</button>
                              </div>
                            </div>
                          </div>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

如何使用 API 从模式中删除相应的用户,因为以下不起作用

<button type="button" class="btn btn-danger" data-ng-click="deleteUser(user._id)">Delete</button>

重要的是,模态不是确认删除弹出窗口,而是包含删除按钮将从中删除相应用户的内容的模态。任何帮助将不胜感激。

看来问题已经解决了。我只是 post 在这里回答。按钮的html应该是:

<button type="button" class="btn btn-danger" data-ng-click="deleteUser(userData._id)">Delete</button>
<!-- Use userData._id instead of user._id-->