1

I am new in angular5/6. Have used *ngFor with static data which works fine but table to not updating when fetching data from servers This HTML table is not getting update

{{userList | json}}--this shows []
<div class="container color-light">
<table>
  <tr>
    <th>Id</th>
    <th>Name</th>
  </tr>
  <tr *ngFor="let y of userList">
    <td>{{y.id}}</td>
    <td>{{y.name}}</td>
  </tr>
</table>

but data is received in component

userList:JsonListInterface[]=[];
  ngOnInit() {
this.callingDataFromHttp();}

callingDataFromHttp(){
 this.listS.getData().subscribe(function(response){
   this.userList = response;
   console.log(this.userList);//this shows proper array with id and name
 });}

can anyone tell where i am going wrong, as frontend table is empty even after getting data.

mahipal singh
  • 355
  • 3
  • 15

1 Answers1

0

TS

    userList: object;
    ngOnInit() {
        this.callingDataFromHttp();
    }

    callingDataFromHttp(){
        this.listS.getData().subscribe(response =>{
            this.userList = response;
         });
    }

HTML

<div class="container color-light">
<table>
  <tr>
    <th>Id</th>
    <th>Name</th>
  </tr>
  <tr *ngFor="let user of userList">
    <td>{{user.id}}</td>
    <td>{{user.name}}</td>
  </tr>
</table>
K. Ayoub
  • 406
  • 1
  • 5
  • 15