0

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?

asked Aug 21, 2017 at 6:29
3
  • 1
    After using delete, the length of the array didn't change? Did you try logging the array in console.Did you try splice? Commented Aug 21, 2017 at 6:31
  • @Vivz not yet, is it makes change on length? Commented Aug 21, 2017 at 6:33
  • Splice will remove the object from the array but how are you trying to remove it? Commented Aug 21, 2017 at 6:35

1 Answer 1

3

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.

answered Aug 21, 2017 at 6:33
Sign up to request clarification or add additional context in comments.

3 Comments

It's better send object to remove function instead index.
@Hadi Why is it better?
@Bata,Because if you are using filter in ng-repeat it's have unexpected result. after filtering you send index to the function now the index that it was sent not real object.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.