Loop through Observable data, push to array, and display all results from array typescript

后端 未结 2 699
南旧
南旧 2021-01-21 05:20

How do i loop through my data, that i subscribed to as an Observable, push it to an array, and displaying the whole data of the array? My present code only displays data from ea

2条回答
  •  天涯浪人
    2021-01-21 05:34

    So this is the final fix (note: the hitsArray is being loaded as an empty array, every time the params changes):

    Component:

    this.storiesService.getData(this.page, this.hits, this.feed)
      .subscribe(
      (data) => {
        console.log(data);
        if (!data || data.hits == 0) {
          this.finished = true;
          console.log("No more hits :-(")
        } else {
          this.finished = false;
          for (let story of data.hits) {
            this.hitsArray.push(story);
            console.log("Hit me!")
            console.log(this.hitsArray);
          }
        }
      })
    

    HTML:

     

    {{story.storyCity}}, {{story.storyCountry}}

    {{story.storyHeadline}}

    Uploadet {{story.uploadDate}}

    Bruger: {{story.userDisplayName}}

    Like Button

    Many thanks to @Theophilus for the suggestion! His example may work in most situations other than mine!

提交回复
热议问题