Farzan Najipour
Farzan Najipour

Reputation: 2503

Angular - delete object from array completely

I have an array like this :

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

I decided to remove single object (with ID=3) from my array. I've tried with delete myArray[1] but it did not change length of my array. Since ng-repeat depends on length of array it makes a big problem.

<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"
                >

It shows three list items which one of them is empty however I want to show couple of them cause I've already delete one of them.

Any suggestion?

Upvotes: 0

Views: 183

Answers (1)

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

Just try something like this:

    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>

Look at this simple example here.

Upvotes: 3

Related Questions