The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Building an Angular App: Deleting an item from a collection

Building an Angular App: Deleting an item from a collection

3:50
We are going to complete the bookmark management functionality by showing how to delete a bookmark from the bookmarks collection. The source for this lesson can be [found on Github](https://github.com/eggheadio/egghead-angularjs-from-scratch-getting-started/tree/07-delete-bookmark). Tags correspond to the lesson.
Watch this lesson now
Avatar
egghead.io

We are going to complete the bookmark management functionality by showing how to delete a bookmark from the bookmarks collection.

The source for this lesson can be found on Github. Tags correspond to the lesson.

Avatar
Linauson Hazell

I haven't tried this out myself, but wouldn't the delete function break the create, since the create is based on the length of the array?

In reply to egghead.io
Avatar
Paul

It would. If you use the current version of lodash (or 4.0.0 onward), you can use _.maxBy(). You could then create a helper function to get the new id and this might look like this:

var getBookmarkId = function(){
    var item = _.maxBy($scope.bookmarks,function(item){
        //will return the object from $scope.bookmarks with the highest id
        return item.id;
     });
     return item.id + 1;
};

Then from within createBookmark you can change bookmark.id = $scope.bookmarks.length; for bookmark.id = getBookmarkId();

In reply to Linauson Hazell
Avatar
Durgesh

Hi,

Can we use the below code to delete the item from the list?

bookmarks.splice($index,1) on dom class "close".

Is it not correct way to go on production??

Thanks
MG

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?