0

component.ts file

posts= [];
  constructor( private http:HttpClient) { 
    http.get('https://jsonplaceholder.typicode.com/posts')
    .subscribe(response=>{
      this.posts.push(response)
      console.log(this.posts)
    });
  }

in HTML file

<ul class="list-group">
  <li 
    *ngFor="let post of posts"
    class="list-group-item">{{post.title}}</li>
</ul>

I can see the data from array if I consoled but for some reason the *ngFor only displays one blank container.

5
  • What does your console shows for response data? Commented Sep 29, 2020 at 7:26
  • I think your code should be this.posts = response, not this.posts.push(response); Commented Sep 29, 2020 at 7:26
  • The response from the URL is an array. At the moment you're having an array of an array: [[{}, {}, ... ]]. You need to assign the array to the variable instead of pushing it: this.posts = response. Commented Sep 29, 2020 at 7:28
  • i checked the type of response from URL and its an object. Commented Sep 29, 2020 at 7:34
  • 1
    It is not an object, it is an array. Commented Sep 29, 2020 at 7:42

2 Answers 2

1

The response of https://jsonplaceholder.typicode.com/posts is array of objects

[
  {
   userId: 1,
   id: 1,
   title: "sunt aut facere repellat",
   body: "quia et suscipit suscipit recusandae consequuntur"
  },
 {
   userId: 1,
   id: 2,
   title: "qui est esse",
   body: "est rerum tempore vitae sequi"
 },
]

so instead of pushing array of object within array, all you need to assign the response.

Create a posts interface instead of using any[] type casting

export interface IPosts = {
  userId: number;
  id: number;
  title: string;
  body: string;
}

Now fetch data from end point

posts: IPosts[]; // notice the type instead of any we have proper type.

constructor(private http:HttpClient) { 

const url = 'https://jsonplaceholder.typicode.com/posts';
http.get(url).subscribe((response: IPosts[]) => this.posts = response)}

And in template just iterate the result.

<ul class="list-group" *ngIf="posts.length"> <=== for safe side you could use if condition only show if posts are available.
  <li 
    *ngFor="let post of posts"
    class="list-group-item">{{post.title}}</li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

0

The response from https://jsonplaceholder.typicode.com/posts is an array of objects. But you are using Array.push() to add items to posts array. So the posts array will have only 1 item (which is array of objects) and that item will not have title property.

Try doing this:

  posts = [];

  constructor( private http:HttpClient) { 
    http.get('https://jsonplaceholder.typicode.com/posts')
    .subscribe(response=>{
      this.posts = response
      console.log(this.posts)
    });
  }

You can also use async pipe in this situation

 posts = this.http.get('https://jsonplaceholder.typicode.com/posts');

 constructor( private http:HttpClient) {}



<ul class="list-group">
  <li *ngFor="let post of posts | async" class="list-group-item"> 
     {{post.title}}
  </li>
</ul>

7 Comments

its giving an error: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.
You can try type casting: .subscribe((response: any[]) =>{this.posts = response });
@HarunYilmaz checkout my answer that is the proper way to use type instead of any etc.
@HarunYilmaz feel free to upvote my answer if you like the approach :)
@KamranKhatti I misread the part any[] :) You're right.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.