0

I have implemented a confirm button where the confirm button will remove a user from the list. But for some reason, it does not remove the user from the list. Can anyone check my js code to see what I have done wrong.

Here is my code

$scope.doDelete = function(user) {
    var index = $scope.userInfo.users.indexOf(user);
    $scope.userInfo.users.splice(index, 1);
    $window.location.href = '#/user';
}

html

<button class="delete" ng-click="doDelete(person)">Confirm</button>
18
  • What does index log out before you splice? What does your array look like? Commented Jun 6, 2017 at 22:31
  • @SterlingArcher Sorry, I can't understand what you've just said. You want me to console.log(indexOf); ? Commented Jun 6, 2017 at 22:32
  • After index is assigned (the line below) do console.log(index) and make sure you're passing a valid index to splice. It will also help to see what $scope.userInfo.users logs out as well Commented Jun 6, 2017 at 22:34
  • So, I get a the index id number. Such as 1. Commented Jun 6, 2017 at 22:36
  • 2
    It doesn't "work" because you reload the page with $windows.location.href. But you're doing it right. Just you're reloading your page, so the data is reinitialised Commented Jun 6, 2017 at 22:39

1 Answer 1

2

You remove the item using:

$scope.userInfo.users.splice(index, 1);

This only removes it in memory (this change isn't persisted). You then reload the page using:

$window.location.href = '#/user';

So your array will be reset back to what it was before you removed the item.

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

11 Comments

should I erase what I have in doDelete Function and just paste the one you wrote?
if yes, it still does not answered my question
I want the changes to take place
Nowhere are you showing how you get $scope.userInfo.users in the first place or even what it contains or where it's located. Maybe it's in a different scope altogether, but we can't know that because we are not mind readers.
The obvious thing to do is console.log($scope.userInfo.users); var index = $scope.userInfo.users.indexOf(user); $scope.userInfo.users.splice(index, 1); console.log($scope.userInfo.users); or set a breakpoint, check the value, step forward, check the value. There is no apparent to why splice wouldn't work in here. So the problem lies with something you haven't shown us.
|

Your Answer

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