how to edit an angular array

Asked

Viewed 432 times

0

Hello I’m new to js and angular and I’m trying to edit an array where I save tasks to be developed. I have the following functions to add and delete the elements, but I can’t think of a way to edit my tasks after registering.

I have the following code until the moment:

Html {{app}}

    <tr ng-class="{selecionado: item.selecionado}" ng-repeat="item in tarefas">
      <td><input type="checkbox" ng-model="item.selecionado"></td>
      <td >{{item.titulo}}</td>
    </tr>
  </table>
</hr>
<form ng-submit="adicionarTarefa(item)">
<input class="form-control" type="text" ng-model="item.titulo" placeholder="Escrever nova tarefa.." ng-keyup="$event.keyCode == 13 && (adicionarTarefa)"/>

Delete task

//Meu array

$scope.tarefas = [
    {
        titulo: "Fazer o documento do carro",
        descricao: "Ano 2015, 2016 e 2017",
        hora: "28/11/2017"
    },
    {
        titulo: "Solicitar emprestimo bancario",
        descricao: "Ano 2015, 2016 e 2017",
        hora: "28/11/2017"
    }
];

//Comportamento do ng click - Cadastro tarefas

    $scope.adicionarTarefa = function(item){
      $scope.tarefas.push(angular.copy(item));
    };

  //Comportamento do ng click - Apagar tarefas
      $scope.apagarTarefa = function(tarefas){
        $scope.tarefas = tarefas.filter(function(item){
          if (!item.selecionado) return item;
        });
      };

});
  • 1

    If you do the bindig item you want to edit in control html, it will update the item as soon as you change on the page, no need to write specific code for it. Something like this would already work: <input type='text' ng-model='tarefas[0].titulo' />, in this example by editing the first item of your array

  • When you say "edit", you mean add/remove (as you are already doing) or modify the contents of each item in the array?

No answers

Browser other questions tagged

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