Data editing problem - Angularjs

Asked

Viewed 29 times

1

I have a grid with the data and also has an edit button.

Problem: When I click on change and start modifying the data, there on the grid the data is being changed too, even without having saved.

My function that loads the data is this:

 /* Função que carrega na grid, TODOS os pacientes cadastrados */
 $scope.loadPacientes = function(){
    OdontoService.load('rest/paciente/loadPacientes').then(function (data) { 
        $scope.pacientes = data.data;

        /* For para formatar a data no formato certo */
        for(var x = 0; x < $scope.pacientes.length; x++){
            $scope.pacientes[x].dataNascimento = new Date($scope.pacientes[x].dataNascimento);
        }

    },function (error){
        console.log(error);
    });
}

I wanted the data to be changed only on the grid, when I saved and not when I am editing yet.

  • This is probably because the data in the grid is associated with the same "model" in both the editing and grid, what you can do is use an auxiliary model only for editing and assign the value only when confirming the editing.

  • @Lucascosta and it is good practice to do this?

  • yes @Guilhermenass, if you want this behavior I see no other alternative, because if a scope variable is being used in n places, when it is changed all the places that are displaying it will be changed as well, blame the Binding :p date

  • @Lucascosta Got it man, hehe. Thanks a lot for the help! .

1 answer

0


If the scope variable is being used in more than one place, if modified, it will show the change where it is being applied to view, due to data Binding which automatically synchronizes the data between the model and the view.

To prevent this situation in your scenario, you can create an auxiliary scope variable and associate the edition.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.