Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic 5 - ngFor not showing data

I tried all ways to display data from the database. Data is coming from the database however it doesn't display any records on the UI. When i print the console.log for data, it comes correctly. Following are the code segments that i have used and i am completely new the ionic development, so kindly point if i made any blunders.

tab3.page.html

<ion-header [translucent]="true">
  <ion-toolbar>
    <ion-title>
      User List
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content [fullscreen]="true">
  <ion-button expand="block" (click)="getData()" >Get All users</ion-button>  
    <ion-list>
      <ion-item *ngFor="let Users of UserList">
            <h2>Hello {{ UserList[Users].fullname.value }}</h2>
            <p item-right> {{Users["email"]}}</p>
            <p>{{Users.phone_no}}</p>
      </ion-item>
    </ion-list>
</ion-content>

tab3.page.ts

import { Component } from '@angular/core';
import { ToastController, AlertController, LoadingController, NavController } from '@ionic/angular';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import {} from '../Login/tab1.module';
import { CommonModule } from "@angular/common";
import { setIndex } from '@ionic-native/core/decorators/common';

@Component({
  selector: 'app-tab3',
  templateUrl: 'tab3.page.html',
  styleUrls: ['tab3.page.scss']
})
export class Tab3Page {

    UserList:any[];
    LoginName:string = ""; 
    constructor(public navCtrl: NavController, public http: HttpClient) {

    }

    ionViewDidLoad(){
      console.log("Getting Users");
      this.getData(); 
    }

    getData()
    {
      this.http.get("http://localhost/MyFirstApp/GetAllUsers.php")
        .subscribe(
          data => {
                    let UserList = JSON.parse(JSON.stringify(data));
                    console.log(UserList);
                    console.log(UserList[1].fullname);
                    let i=0;
                    for (let Users in UserList){
                        this.LoginName = UserList[Users].fullname;
                    } 

                  }, 
          err => {
                    console.log(err);
                  }
      ); 
    }


}

Data is coming correctly from Database in console.log window

UserList[
0: {fullname: "shubham", email: "[email protected]", phone_no: "1230235420"}
1: {fullname: "johan", email: "[email protected]", phone_no: "120356452"}
2: {fullname: "aditya", email: "[email protected]", phone_no: "120356452"}
]
like image 730
Ankur M Avatar asked May 16 '20 15:05

Ankur M


2 Answers

Three main things:

  1. You have used let which is a block scope, hence would not be reflected in the class.

    Solution: Change let UserList = JSON.parse(JSON.stringify(data));
    to this.UserList = JSON.parse(JSON.stringify(data));.

  2. You have a binding issue, since you are not placing the index in array value, but instead are passing an object value.

    Solution: Change {{ UserList[Users].fullname.value }}
    to {{Users.fullname}}.

  3. You have only type annotated the UserList object, since it is used to iterate.

    Solution: Change UserList:any[];
    to UserList:any[]=[];.

like image 85
Omkar Tondawalkar Avatar answered Oct 11 '22 07:10

Omkar Tondawalkar


You are doing some wrong reference to the variables. I guess only phone_no should be displaying. You are referencing as arrays, but they are part of a JSON Object.

The right way should be:

{{Users.fullname}}
{{Users.email}}
{{Users.phone_no}}

And, as final note, please DO NOT name variables starting with capital letter. Let be user instead of User. Capital letters for classes only. Maybe, and I say maybe, the code is trying to look for Users class, and that is why it is not working....

like image 41
Sergio Avatar answered Oct 11 '22 07:10

Sergio