2017-03-06 111 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; 
    }); 
}