user3430365
user3430365

Reputation: 37

list does not update after deletes the angularjs

after I click the button to delete the list does not update, the data is deleted from the database, only after I do an update on the page is the list updated.

code html:

<table class="table table-hover">
                    <tr>
                        <th>#</th>
                        <th>Nome</th>
                        <th>Email</th>
                        <th>Administrador</th>
                        <th>Login</th>
                        <th>Remover</th>
                    </tr>
                    <tr ng-repeat="usuario in usuarios | filter: global.search">
                        <td>{{$index+1}}</td>
                        <td><a ng-href="#/edita/usuario/{{usuario._id}}">{{usuario.nome}}</a></td>
                        <td>{{usuario.email}}</td>
                        <td>{{usuario.administrador}}</td>
                        <td>{{usuario.login}}</td>
                        <td><button ng-click="removeUser(usuario)" class="btn-link"><i style="float: right;" class="icon-remove"></i></button></td>
                    </tr>
                </table>

controller angular:

$scope.removeUser = function(usuario){

    var confirmar = confirm("Tem certeza que deseja excluir usuário?");

    if(confirmar == true){
        Usuario.delete({id: usuario._id},
            buscaUsuarios(),
            function(erro){
                console.log("Não foi possível remover usuário.");
                console.log(erro);
            });
    }
    console.log(usuario);
};

controller node:

controller.removeUsuario = function(req, res){

    var id = req.params.id;
    console.log("IDREMOVE "+id);
    Usuario.remove({_id : id}).exec()
        .then(
            function(){
                res.status(204).end();
            },
            function(erro){
                return console.error(erro);
            });

};

Thanks.. :)

Upvotes: 0

Views: 59

Answers (3)

user3430365
user3430365

Reputation: 37

Hello excuse me by incomplete code.I was trying to pass a callback code to return the updated list. Below the elbow that was passing as callback:

function buscaUsuarios(){

    Usuario.query(
        function(usuarios){
            $scope.usuarios = usuarios;
        },
        function(erro){
            console.log('Não foi possível obter a lista de usuários.');
            console.log(erro);
        });
};

Upvotes: 0

GPicazo
GPicazo

Reputation: 6676

It is not clear what is happening because you are not including enough code. However, it seems that you are deleting the user asynchronously trying to pass a success callback handler (buscaUsuarios) and an error handler. Instead of passing the success callback which retrieves the new list, however, it looks like you are passing the results of executing it. Try this instead:

$scope.removeUser = function(usuario){

    var confirmar = confirm("Tem certeza que deseja excluir usuário?");

    if(confirmar == true){
        Usuario.delete({id: usuario._id},
            buscaUsuarios,
            function(erro){
                console.log("Não foi possível remover usuário.");
                console.log(erro);
            });
    }
    console.log(usuario);
};

Upvotes: 0

justinaray
justinaray

Reputation: 128

It's not clear from your code, but you're probably not updating the array usuarios in your scope.

Check to make sure you are actually modifying that reference. Also note that you should modify the reference itself, not replace it with a new array instance.

Upvotes: 1

Related Questions