javascript - Angular - 从数组中完全删除对象

标签 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"
  __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/

相关文章:

javascript - 隐藏 AmCharts 外部数据 URL

ios - 访问 CKRecordZone 数组中的字段?

PHP:从 XML 字符串创建数组

javascript - d3v4 如何将条形图设置为不同的颜色

javascript - 类型不匹配 - JavaScript

javascript - 它是如何工作的将空数组插入空数组

javascript - angular-ui bootstrap typeahead 转义键导致输入字段值变得未定义

javascript - 如何在 Angular 表单上设置自定义验证?

AngularJS ng 类表达式未正确更新

javascript - 需要显示for循环数组中的所有数字