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.
<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>
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"}
]
Three main things:
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));
.
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}}
.
You have only type annotated the UserList
object, since it is used to iterate.
Solution:
Change UserList:any[];
to UserList:any[]=[];
.
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With