AngularJs – Delete the card that i have clicked inside ng-repeat

I have an array of cards displayed whit an ng-repeat. I’m passing the id to my function, im using $timeout because i need to delete the card 5 seconds after the click , now the objective is to delete the card that I have clicked. Thanks.

JS

$scope.includeCard = function(id){

  $timeout(function(){ 

      console.log(id) 

  },2000)

}

My html

    <div class="cards"> 

     <div class="" ng-repeat="card in  cards" >

      <div class="card-layout"> 

        <div>Card Example</div>

        <div ng-click="excludeCard(card.id)"></div>

      </div> 

    </div>

  </div>

Answer

You could pass the index of the object that you want to remove and remove it from the array using “splice” function. Try this:

JS

$scope.includeCard = function(index){

  $timeout(function(){ 

      $scope.cards.splice(index, 1);

  },2000)

}

HTML

<div class="cards"> 

 <div class="" ng-repeat="card in  cards" >

  <div class="card-layout"> 

    <div>Card Example</div>

    <div ng-click="excludeCard($index)"></div>

  </div> 

</div>

Leave a Reply

Your email address will not be published. Required fields are marked *