0

I have ng-click event:

removeFile(file, $index);

Where file is object:

File
$$hashKey:"object:572"
lastModified:1487594253749
lastModifiedDate:Mon Feb 20 2017 15:37:33 GMT+0300 (RTZ 2 (зима))
name:"1 — копия — копия — копия.jpg"
size:315074
type:"image/jpeg"
webkitRelativePath:""

I try to remove object mentioned above from array Files:

I tried:

delete $scope.files[index];
Daniel
  • 1,695
  • 6
  • 23
  • 41
  • 4
    Possible duplicate of [How do I delete an item or object from an array using ng-click?](http://stackoverflow.com/questions/15453979/how-do-i-delete-an-item-or-object-from-an-array-using-ng-click) – Smit Mar 06 '17 at 07:18
  • Judging by the accepted answer, it seems like you are asking how to remove an element from an array, rather than an object. – Paul Rooney May 07 '18 at 00:47

3 Answers3

1

You can use splice :

$scope.removeFile = function(file){
    var index = $scope.files.indexOf(file);
    $scope.files.splice(index,1);
}
Jenny
  • 663
  • 4
  • 8
0

In place of delete use splice

var index = $scope.files.indexOf(index);
  if(index>=0)
    $scope.files.splice(index, 1);
}
Manoj Patidar
  • 1,171
  • 2
  • 11
  • 29
0

best way to do this using filter function of array

$scope.removeFile = function(file){
    $scope.files = $scope.files.filter(function(fileItem) {
        return fileItem.name != file.name;
    });
}
Gaurav Kumar Singh
  • 1,550
  • 3
  • 11
  • 31