Skip to main content

Command Palette

Search for a command to run...

Angular : Output data from get Request.

Published
1 min read
Angular : Output data from get Request.
S

I love learning about technology and sharing that with others

introduction

so after making a get request, we are storing all the posts the loadedPosts array and then in template HTML we are checking if array length created than 1 traverse the array and show the values .

HTML template

 <div class="row">
    <div class="col-xs-12 col-md-6 col-md-offset-3">
      <p *ngif="loadedPosts.length<1">No posts available!</p>
      <ul class="list-group" *ngIf="loadedPosts.length>=1">
        <li class="list-group-item" *ngFor="let post of loadedPosts">
          <h3> {{post.title }} </h3>
          <p> {{post.content}}</p>
        </li>
      </ul>
    </div>
  </div>
</div>
 private fetchPosts() {

    this.http.get< { [key:string]:Post  } >('https://ngfirstproject-default-rtdb.asia-southeast1.firebasedatabase.app/posts.json')
    .pipe( 
      map(responseData => {

      const postArray=[];

      for (const key in responseData) {
        if(responseData.hasOwnProperty(key)) {
        postArray.push( { ...responseData[key] , id:key} )
        }
      }
      return postArray;
    })

    )
    .subscribe( posts => {
        this.loadedPosts=posts;
    }
    );
  }
}
3 views

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)

Angular : Output data from get Request.