如何從基於過濾器的數組中刪除元素,只在'quantity'爲0且'isSelected'爲false時刪除這些索引?任何建議。這裏是我的代碼:基於angularjs中的過濾器從陣列中刪除元素
<table data-ng-repeat="st in Absorb track by $index" data-ng-cloak data-ng-init="remove(st)">
<tr>
<td>
<input type="radio" name="groupName" data-ng-model ="st.isSelected" data-ng-checked="(st.isSelected == true)"/>
</td>
<td>
<span>{{st.ProjectedQuantityOnHand}}</span>
</td>
<td style="display:none" data-ng-if="st.ProjectedQuantityOnHand == 0">
<input type="number" data-ng-model="st.ProjectedQuantityOnHand" style="display:none">
</td>
</tr>
</table>
JS代碼:
$scope.Absorb = [
{"Name":"apple", ProjectedQuantityOnHand:"0", isSelected:true},
{"Name":"mango", ProjectedQuantityOnHand:"0", isSelected:false}
{"Name":"ball", ProjectedQuantityOnHand:"1", isSelected:false}
{"Name":"football", ProjectedQuantityOnHand:"1", isSelected:false}
]
$scope.remove = function (item) {
var availableqauantity = 0
debugger
angular.forEach($scope.StockList, function (i) {
var FilteredProduct1 = $filter('filter')($scope.StockList, { isSelected: false, ProjectedQuantityOnHand: 0 });
if (FilteredProduct1.length > 0) {
availableqauantity = FilteredProduct1[0].ProjectedQuantityOnHand;
if (availableqauantity == 0)
$scope.StockList.splice(i,1);
}
});
}
你真的想從數組中刪除這些項嗎?還是你想讓它們不顯示? – Eddi
@Eddi你只是不想爲angulars 1.x或2顯示 – Felixerity
?在角度2中,您可以在組件的類中進行篩選,而不是在使用篩選器管道的模板中進行篩選。 –