Can you change your JSON to have a loaded boolean property? If possible, do this:
<ion-card *ngFor="let item of items"> <img [src]="item.picture" (load)="item.loaded = true" [hidden]="!item.loaded" /> <img src="../path/to/your/placeholer/image.jpg" [hidden]="item.loaded"> </ion-card>
You will have a loaded property, which is always initialized to false , when the image loads an event (load) , it will fire and change the property of the loaded image to true, hide the placeholder image and display the loaded image.
If you cannot change this JSON (or if it is too large for editing), you can use the above code, but on the .ts page
follow these steps:
public functionWhoseIsLoadingYourJson(){
Just repeat your answer, and in each object set the loaded property to false. If this leads to some error, you can put your <ion-card> inside the div using * ngIf
<div *ngIf="this.items != null"> </div>
EDIT - JSON Update
Since Firebase has three options for updating your JSON. The first one is a function that you would execute once to update your database, and the second is the code that I gave (iterating through the results and pressing a boolean value), but it will be different, since you use Firebase.
First decision:
Somewhere in your code, it could be any page, you just need to execute it once:
firebase.database().ref('myDataNode').on('value', snapshot => { for(let key in snapshot.val()){ firebase.database().ref('myDataNode/' + key + '/loaded').set(false); } });
See that I myDataNode over all the options in myDataNode and use their key to set the loaded property to false . But be careful when using set , since you can override all your nodes before doing this method, remember that you are exporting a Firebase node to have a backup. And if you click your images on Firebase from anywhere in your code, remember to change it to set the loaded property together.
The second, as already mentioned, is what I did in this answer, but now I use it inside your Firebase call and using the forEach method that comes with the snapshot:
firebase.database().ref('myDataNode').on('value', snapshot => { snapshot.forEach(element =>{ this.myDataNode.push({ picture: element.val(),
This will add a new element to your array with the image URL and property loaded. If it cannot recognize the .push() method, just declare your variable printed as an array:
public myDataNode: any[];
The third one is simply updated locally, repeated through a firebase snapshot, saves the result in a local variable, creates a new property and inserts it into your array:
firebase.database().ref('myDataNode').on('value', snapshot => { for (let key in snapshot.val()){ snapshot.forEach(item => {
Hope this helps.