3

I have a service that is getting data from the server

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from "rxjs/Observable";

@Injectable()
export class CardService {

    private url = "http://192.168.100.112:8080/angular";

  constructor(private http: HttpClient) { }

  getCards(): Observable<any>{
  return this.http.get<any>(this.url);
 }
}

And it works fine with my component:

import { Component, OnInit } from '@angular/core';
import { CardService } from '../../services/card.service';

@Component({
    selector: 'cards-list',
    templateUrl: './card.component.html',
    styleUrls: ['./card.component.css']
  })
  export class CardComponent implements OnInit {

  private cardData: any;

  constructor(private svc: CardService) { }

  ngOnInit() {
    this.svc.getCards().subscribe(data =>{
      this.cardData = data;
      console.log(this.cardData)
    })
  }
}

The console is displaying the data I am trying to show on the app.component.html

but the data is not being displayed on the website:

appcomponent.html

<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
  <cards-list></cards-list>
</div>

card.component.html

<div *ngIf="cardData">
  <ul>
    <li *ngFor="let card of cardData">
       <span>{{card.name}}</span>
       <span>{{card.id}}</span>
       <span>{{card.progress}}</span>
       <span>{{card.issue}}</span>
    </li>
  </ul>
</div>

Does anyone know what I am doing wrong?

enter image description here

7
  • I forgot to write that before, I updated the question. Commented Jun 21, 2018 at 11:28
  • cardData is an array of JSON ? Commented Jun 21, 2018 at 11:30
  • Yes, it is. I added my console log information Commented Jun 21, 2018 at 11:31
  • 1
    change private cardData: any; to private cardData:any[] = [];. May this help you Commented Jun 21, 2018 at 11:32
  • 2
    @Fix3r , your data is a simple object . You cannot loop over a simple object Commented Jun 21, 2018 at 11:32

3 Answers 3

3

ngFor required an array for loop through. Response coming as an object. make carddata an array and push the response object.

  private cardData: any = [];

  constructor(private svc: CardService) { }

  ngOnInit() {
    this.svc.getCards().subscribe(data =>{
      this.cardData.push(data);
      console.log(this.cardData)
    })
  }
Sign up to request clarification or add additional context in comments.

4 Comments

Your first suggestion was this.cardData = [data]; and it worked great. What is the different now? Thanks for the answer
@SachilaRanawaka, this will fail if API returns the multiple objects in a form of list next time.
if it return an array no need to push. just assign the array object to carddata, it all depend on the response type
@SachilaRanawaka using the suggested code and based on what you and Prachi said I got it working. Thank you guys
3

This code will not work for a single object. You need to return the array even if the record length is 1.

<div *ngIf="cardData">
  <ul>
    <li *ngFor="let card of cardData">
       <span>{{card.name}}</span>
       <span>{{card.id}}</span>
       <span>{{card.progress}}</span>
       <span>{{card.issue}}</span>
    </li>
  </ul>
</div>

4 Comments

It actually can return more than one object as well
Then you need to change your API, not your code. Change your API to return data in the form of list, even if the record length is 1. Currently, if record length is 1, its returning object, instead of array of objects.
What do you mean? On the code, I have now an array with two json objects. The solution suggested above only works for one, as stated before.
I stated the same. Use your angular code only and make changes in API in case of record length 1.
-2

Define, private cardData: any[]=[];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.