Javascript 角度-从数组中完全删除对象
我有这样一个数组:Javascript 角度-从数组中完全删除对象,javascript,angularjs,arrays,Javascript,Angularjs,Arrays,我有这样一个数组: 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" _
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:
-
去除
看看这个简单的例子 试试这样的方法:
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:
-
去除
看看这个简单的例子 使用delete后,数组的长度没有改变?您是否尝试在控制台中记录阵列。您是否尝试了splice?@Vivz尚未,它是否更改了长度?splice将从阵列中删除对象,但您如何尝试删除它?使用delete后,阵列的长度没有更改?您是否尝试在控制台中记录数组。是否尝试了splice?@Vivz还没有,它是否更改了长度?splice将从数组中删除对象,但您如何尝试删除它?最好将对象发送到
remove
函数,而不是index。@Hadi为什么更好?@Bata,因为如果您在ng repeat
中使用过滤器,可能会产生意外的结果。过滤后,您现在将索引发送到函数,它被发送的索引不是真实的对象。最好将对象发送到remove
函数,而不是index。@Hadi为什么更好?@Bata,因为如果您在ng repeat
中使用过滤器,它会产生意外的结果。过滤后,您将索引发送到函数,现在发送的索引不是真实对象。