1

I have the following array in my controller that gets objects from the database:

this.objects = [ //data of array goes here ];

Then I have an html table:

<tbody ng-repeat="object in object.recipients>
    <tr>
        <td>{{object.attribute.content}}</td>
    </tr>
</tbody>

The table is displayed correctly. And I have a delete button that removes objects from the database. But in order to see the changes on my table, I need to refresh the page. How can I have my table updated without refreshing?

3 Answers 3

1

Just delete using a controller function that does:

this.objects.splice(index, 1)

and in your delete button in html, call the delete function and pass the index, or another unique id. e.g

<button ng-click="deleteThis($index)">Delete</button>

$index refers to the current index in the ng-repeat

If you prefer, you can delete from your server, and if success callback/promise is resolved, then delete from the view as explained above

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Although all answers given were correct, I did not know about the index thing. I was searching through my array for that element to delete it. So +1 for that!
Don't wanna be a buzzkill, but you should also be aware of this when relying on $index: codementor.io/angularjs/tutorial/…
1
  1. When you delete record in database. You should reload data from $http and set data for $scope.objects.
  2. Or using delete

    $scope.objects = $scope.objects.slice(1); // 1 is index object

    then delete record in database.

1 Comment

Thank you for your reply. And for the detail. You are correct.
1

In your delete item function, after splicing the item from the array, save the data to your db, and then immediately use ajax to GET the new data set back. Once finished saving the data to the db, use:

$http.get(...)

to fetch the new data and update the array/table (the way it should be) without having to refresh the page.

Comments

Your Answer

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.