I have a page where I display all the Persons from the DB, let's call it All Persons Page. On that page I have 2 options:
- Add New Person
- Delete Person (Whatever Person I want)
The problem appears when I am navigating to another page and then get back to the All Persons Page.
If I Delete a Person and after that Add a New Person, the view will display the New Person added but also the Deleted Person.
In the controller I am adding the data from the DB in an array.
$scope.persons_array = []
And display in the view via ng-repeat.
<div ng-repeat="(key, value) in persons_array)">
To Add a Person I am inserting a new ID in the array:
$scope.persons_array.push($scope.id[i])
To Delete a Person I am removing the spcified ID from the array:
$scope.persons_array.splice(i,1);
Tests:
To discover what the problem is I've created an setInterval function:
setInterval(function(){
console.log($scope.persons_array)
}, 5000)
When I was on the All Persons Page, where I already had a Person added, I could see:
[Object { id=667, $$hashKey="object:71"}]
When I navigate to another page and then get back:
[Object { id=667, $$hashKey="object:71"}] //original
[Object { id=667, $$hashKey="object:220"}] //new
Add a New Person:
[Object { id=667, $$hashKey="object:71"}] //original
[Object { id=667, $$hashKey="object:220"}, Object { id=668, $$hashKey="object:227"}] //new
Detele the Person just added:
[Object { id=667, $$hashKey="object:71"}] //original
[Object { id=667, $$hashKey="object:220"}, Object { id=668, $$hashKey="object:227"}] //new
Delete the Person that was already in the DB:
[] //original
[Object { id=667, $$hashKey="object:220"}, Object { id=668, $$hashKey="object:227"}] //new
Add Again a New Person:
[] //original
[Object { id=667, $$hashKey="object:220"},
Object { id=668, $$hashKey="object:227"},
Object { id=669, $$hashKey="object:235"}] //new
From what I can see, In the moment I am navigating to another page, Angular is duplicating in some way the $scope.persons_array and after that is no longer updating the original array.