Hide ng-repeat item after 5 seconds

Asked

Viewed 87 times

2

I am making a task app to do (To from the list), I would like to know how to hide an item after 5 seconds after selecting the item through my checkbox.

I have the following HTML structure:

<body ng-controller="todoListCtrl">
  <div class="container">
    <div class="jumbotron">
      <h4><img src="img/list.png" id="icon-list"> {{app}} </h4>
      <table class="table ">
        <tr ng-class="{selecionado: item.selecionado}" ng-repeat="item in tarefas">
          <td><input type="checkbox"  ng-model="item.selecionado"></td>
          <td><input  type="text" ng-class="{selecionado: item.selecionado}"class="form-control" id="item" ng-model="item.titulo" ng-blur="item.modoEdicao = false" ng-focus="item.modoEdicao = true" ></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)"/>
      </form>
      <button class="btn btn-danger btn-block" ng-click="apagarTarefa(tarefas)">Apagar tarefa</button>
    </div>
  </div>
</body>

I have the following code JavaScript so far:

//localizando o modulo
angular.module("todoList").controller("todoListCtrl", function($scope) {
  $scope.app = "Minhas Tarefas";
  $scope.tarefas = [{
      titulo: "Fazer o documento do carro",
      modoEdicao: false
    },
    {
      titulo: "Solicitar emprestimo bancario",
      modoEdicao: false
    }
  ];

  //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;
    });
  };
});

// function autoFocus (){
//   $('#teste').focus();
// };
$(document).ready(function() {
  setInterval(function() {
    $('#item').fadeOut(1500);
  }, 3000);
});

1 answer

2


You can use the service $timeout together with the ng-class to set a class after 5000 milliseconds (5 seconds) which will make the item disappear by assigning the class escondido in the following example:

angular
  .module("todoList", [])
  .controller("todoListCtrl", todoListCtrl);

todoListCtrl.$inject = ['$timeout'];

function todoListCtrl($timeout) {
  var vm = this;
  
  vm.apagar = _apagar;

  vm.classes = [];
  vm.tarefas = [
    {titulo: "Fazer o documento do carro"},
    {titulo: "Solicitar emprestimo bancario"}
  ];

  function _apagar(indice) {
    $timeout(function() {
      vm.classes[indice] = 'escondido';
    }, 5000);
  };
};
.item {
  transition: all linear 0.5s;
  margin-top = 0px;
  display: block !important;
}
.item.escondido {
  opacity: 0;
  height: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="todoList" ng-controller="todoListCtrl as vm">
  <table class="table ">
    <tr ng-repeat="item in vm.tarefas" class="item" ng-class="vm.classes[$index]">
      <td><input type="checkbox" ng-model="item.selecionado" ng-click="vm.apagar($index)"></td>
      <td><span>{{item.titulo}}</span>
    </tr>
  </table>
</div>

Browser other questions tagged

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