2017-03-06 102 views
0

我有ng-click事件:如何從對象中刪除元素?

removeFile(file, $index); 

哪裏file是對象:

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:"" 

我嘗試刪除從陣列Files上述目的:

我嘗試:

delete $scope.files[index]; 
+4

的可能的複製[如何刪除項目或對象從數組使用ng單擊?](http://stackoverflow.com/questions/15453979/how-do-i-delete-an-item-or-object-from-an-array-using-ng - 點擊) – Smit

回答

1

您可以使用拼接:

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

代替刪除使用拼接的

var index = $scope.files.indexOf(index); 
    if(index>=0) 
    $scope.files.splice(index, 1); 
} 
0

最好的辦法陣列的這種使用過濾功能

$scope.removeFile = function(file){ 
    $scope.files = $scope.files.filter(function(fileItem) { 
     return fileItem.name != file.name; 
    }); 
}