O'Reilly logo

Learning Web Development with Bootstrap and AngularJS by Stephen Radford

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Delete

Deleting our contacts can once again be achieved through our service. We can create a final method that will accept an index of our array and remove it. Since delete is a keyword in JavaScript, let's use destroy as the name for our method:

destroy: function(index){
    contacts.splice(index, 1);
}

It's a very simple method. We're just taking the index and using the native splice method to remove it from the array. Now, we need to create a function on our index view's scope that can call this method from our service:

$scope.delete = function(index){
    contacts.destroy(index);
};

Finally, we can add a button to our actions column in our table to delete the required contact on click:

<button class="btn btn-danger btn-xs" ng-click="delete($index)">Delete</button> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required