I’m having a problem with ngFor and I don’t know why. Let’s assume that I have an array with just two objects.
<ion-card *ngFor="let something of localService.getStuff()">
<ion-card-content>
<strong>{{something.name}}</strong> // This has values "name1" and "name2"
<br/>{{getData(something.id)}} // This has values 1 and 2.
</ion-card-content>
</ion-card>
The first time all the data shown is correct, id’s values are 1 and 2. BUT if I go to another screen and come back, then “something.id” values are 2 and 2 again. However, “something.name” is working fine, it still has values “name1” and “name2”.
So, I tried to rename the field “id” to “di” and magic! all the values of “di” are correct! (1 and 2).
Guessed so. Why are you directly calling localservice.getStuff inside the ngfor btw? Isn’t it easier to just return it inside your component, and loop over the results?
stuff:any;
and load it in the page ionViewDidLoad() { this.stuff = localService.getStuff(); console.log(this.stuff) }
what do you see? Are you sure getStuff doesnt return a promise?
{{ something | json }} shows all fields correct BUT when I come back from the other view, both id are 2. The other fields are ok. I don’t understand why id field changes when I come back but not the other fields.
That’s really weird though. And you’re absolutely sure you don’t manipulate the array whatsoever? What if you just create two different objects inside an array with key-values and use them for testing, still the same issue?
OMG it was a if (something.id = id) instead a if (something.id == id_something) in a method of the service launched on the other screen. So that changed the value in the view, because it’s binded.
I never changed the “id” name in that service’s method, so when I changed the name in my test to “di”, the “if” it was setting nothing, because “id” field didn’t exist. That’s why to change the name fixed it.
With no warnings by the tslint or the VS Code about that mistake, I couldn’t see it until now. Thank you both for make me check the code more closely.