You can't use the function remove
to remove an item from an array.
splice to remove an object from an array
To remove an element from an array you should use splice:
removeUpload(uploadItem) {
// get index/position of uploadItem within array
const index: number = this.uploads.indexOf(uploadItem);
// if index returned is negative it means element not found in array
// else: (positive) index can be used
// e.g. to remove the single element at this position
if (index !== -1) {
this.uploads.splice( index, 1 );
}
}
This removes from index-position exactly one single element (thus second argument is 1
here).
Of course you to have add the argument upload
as argument to button's click-event, so that the function knows which element of the array it has to remove:
<button class="btn" (click)="removeUpload( upload )" title="remove this">x</button>
See demo on stackblitz.
Shortcuts
If you want to remove the first element of the array, use array.shift().
If you want to remove the last element of the array, use array.pop().
Both functions return the removed element.
What to add/remove from the array?
I am not sure why you add/remove (push
respective splice
) the array's length to the uploads
array. Does the array store the current size of itself or rather upload-item-objects ?
See also
A: remove item from stored array in angular 2