我有一个像这样的数组:
Array[3]
0:Object
ID:7
name:"bestRes3t"
profile:"rest7.png"
rate:0
restaurantCitySlug:"NY"
slug:"fo4o"
__proto__:Object
1:Object
ID:3
name:"bestRest"
profile:"rest.png"
rate:1
restaurantCitySlug:"NY"
slug:"foo"
__proto__:Object
2:Object
ID:7
name:"bestR242es3t"
profile:"re3st7.png"
rate:2
restaurantCitySlug:"NY"
slug:"fo244o"
__proto__:Object
我决定从数组中删除单个对象(ID=3)。我尝试过删除 myArray[1]
但它没有改变我的数组的长度。由于 ng-repeat 取决于数组的长度,因此这是一个大问题。
<li ng-repeat="(resNo,restaurant) in myArray" style="margin-bottom: 20px"
dnd-draggable="restaurant"
dnd-effect-allowed="move"
dnd-moved="myArray.splice($index, 1)"
dnd-selected="myArray.selected = resNo"
dnd-callback="myArray.length"
>
它显示了三个列表项,其中一个为空,但我想显示其中几个,因为我已经删除了其中一个。
有什么建议吗?
最佳答案
尝试这样的事情:
app.controller('demoController', function($scope) {
// initial items
$scope.items = [
'item one',
'item two',
'item three'
];
// remove an item
$scope.remove = function(index) {
$scope.items.splice(index, 1);
};
});
HTML:
<div ng-controller="demoController">
<!-- list of items with a button to remove that item -->
<ul>
<li ng-repeat="item in items">
<button ng-click="remove($index)">Remove</button>
</li>
</ul>
</div>
看看这个简单的例子 here 。
关于javascript - Angular - 从数组中完全删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45790557/