简体   繁体   English

遍历 Observable 数据,推送到数组,并显示数组打字稿的所有结果

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

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?我如何遍历我作为 Observable 订阅的数据,将它推送到一个数组,并显示该数组的整个数据? My present code only displays data from each "page", and not all the pages.我目前的代码只显示每个“页面”的数据,而不是所有页面。 The reason why i want to do this, is because i want to make an infinity scroll.我之所以要这样做,是因为我想做一个无限滚动。

Thank you!谢谢!

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: HTML:

 <div class="col-4 col-md-4 col-sm-12" *ngFor="let story of hitsArray">
  <div class="thumbnail">
    <img *ngIf="story.storyMediaType === 'image'" class="img-fluid" src="{{story.storyThumbnailImage}}" />
    <img *ngIf="story.storyMediaType === 'video'" class="img-fluid" src="{{story.storyThumbnailImage}}" width="150" height="94" />
    <div class="caption">
      <p>{{story.storyCity}}, {{story.storyCountry}}</p>
      <h3>{{story.storyHeadline}}</h3>
      <p>Uploadet {{story.uploadDate}}</p>
      <p>Bruger: {{story.userDisplayName}}</p>
      <p><a href="#" class="btn btn-primary" role="button">Like</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
    </div>
  </div>
</div>

Based on your component's template, you are using data which is updated for every newly fetched data from observable here in your subscription根据您的组件模板,您正在使用data ,这些data会针对订阅中从 observable 中每个新获取的数据进行更新

for (let story in data) {
        this.data = data; --> here you update regularly

from the above you are saying for every array key update our this.data which seems wrong, because the keys in an array is 0,1,2 which are the index.从上面你说每个array key更新我们的this.data这似乎是错误的,因为数组中的键是 0,1,2,它们是索引。

Both for..of and for..in statements iterate over lists; for..offor..in语句都遍历列表; the values iterated on are different though, for..in returns a list of keys on the object being iterated, whereas for..of returns a list of values of the numeric properties of the object being iterated.迭代的值虽然不同,但 for..in 返回正在迭代的对象上的键列表,而 for..of 返回正在迭代的对象的数字属性的值列表。 readmore 阅读更多

So instead of the long trip your subscription code that handles the processing of the retrieved data should look like the one below因此,处理检索到的数据的订阅代码应如下所示,而不是长途旅行

for (let story of data) { // now of
       // this.data = data; not needed else your data is always overriden
       // for (let i = 0; i < this.data.length; i++) { no need for this again since the above for..of is handling it
          this.hitsArray.push(story);
          this.data.hits.push(story);// add the new stories(as last element) to the main data array using this.data here now

          console.log(this.hitsArray);
      //  } for loop of this.data.length
      }

So with the above code following the comments should explain what the code is doing and how the irrelevant parts are causing data override.因此,上面的代码后面的注释应该解释代码在做什么以及不相关的部分如何导致数据覆盖。

PS your this.data.hits must be an array initialized as [] when the program is loading the component. PS您的this.data.hits必须是程序加载组件时初始化为[]数组 And your data from your observable must be an array or if object with hits as array then use this code instead for (let story in data.hits) { .并且您的 observable 中的data必须是一个数组,或者如果对象的命中数组,则使用此代码代替for (let story in data.hits) {

Hope this helps.希望这可以帮助。

So this is the final fix (note: the hitsArray is being loaded as an empty array, every time the params changes):所以这是最终修复(注意:每次参数更改时,hitsArray 都会作为空数组加载):

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: HTML:

 <div class="col-4 col-md-4 col-sm-12" *ngFor="let story of hitsArray">
  <div class="thumbnail">
    <img *ngIf="story.storyMediaType === 'image'" class="img-fluid" src="{{story.storyThumbnailImage}}" />
    <img *ngIf="story.storyMediaType === 'video'" class="img-fluid" src="{{story.storyThumbnailImage}}" width="150" height="94" />
    <div class="caption">
      <p>{{story.storyCity}}, {{story.storyCountry}}</p>
      <h3>{{story.storyHeadline}}</h3>
      <p>Uploadet {{story.uploadDate}}</p>
      <p>Bruger: {{story.userDisplayName}}</p>
      <p><a href="#" class="btn btn-primary" role="button">Like</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
    </div>
  </div>
</div>

Many thanks to @Theophilus for the suggestion!非常感谢@Theophilus 的建议! His example may work in most situations other than mine!他的例子可能适用于我以外的大多数情况!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM