How do I delete an item or object from an array using ng-click? How do I delete an item or object from an array using ng-click? angularjs angularjs

How do I delete an item or object from an array using ng-click?


To remove item you need to remove it from array and can pass bday item to your remove function in markup. Then in controller look up the index of item and remove from array

<a class="btn" ng-click="remove(item)">Delete</a>

Then in controller:

$scope.remove = function(item) {   var index = $scope.bdays.indexOf(item);  $scope.bdays.splice(index, 1);     }

Angular will automatically detect the change to the bdays array and do the update of ng-repeat

DEMO: http://plnkr.co/edit/ZdShIA?p=preview

EDIT: If doing live updates with server would use a service you create using $resource to manage the array updates at same time it updates server


This is a correct answer:

<a class="btn" ng-click="remove($index)">Delete</a>$scope.remove=function($index){   $scope.bdays.splice($index,1);     }

In @charlietfl's answer. I think it's wrong since you pass $index as paramter but you use the wish instead in controller. Correct me if I'm wrong :)


In case you're inside an ng-repeat

you could use a one liner option

    <div ng-repeat="key in keywords">         <button ng-click="keywords.splice($index, 1)">            {{key.name}}        </button>    </div>

$index is used by angular to show current index of the array inside ng-repeat