我在控制台中得到一个<item>数组,每个数组都包含标题、链接、描述、图像和发布日期,但数据不显示在ion-list中。
你能帮我吗?
下面是load()中的函数rss-service.ts

load() {
  return Observable.create(s => {
    this.http.get('http://url.xml')
      .map(res => res.text())
      .subscribe(data => {
        if(data) {
          var parser = new DOMParser();
          var xmlData = parser.parseFromString(data, "application/xml");
          var items = xmlData.querySelectorAll("item");
          for (var index = 0; index < items.length; index++) {
            var element = items[index];
            console.log(element);
          }
        }
     });
  });
}

home.ts
export class HomePage {

  public entries: any  = [];

  constructor(public rssService:RssService, public nav:NavController) {
  }

  ionViewDidLoad(){
      this.rssService.load().subscribe(
          data => {
              this.entries.push(data);
          }
      );
  }

  openPage(entry) {
      console.log('open page called with ' + entry.title);
      this.nav.push(DetailPage, {selectedEntry:entry});
  }
}

home.html
<ion-header>
  <ion-navbar color="primary">
    <ion-title text-center>
     App Name
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  </ion-refresher>
    <ion-list>
        <ion-item *ngFor="let entry of entries" (click)="openPage(entry)" text-wrap>
      <h2 class="titles">{{entry.title}}</h2>
    </ion-item>
    </ion-list>
</ion-content>

最佳答案

可观察到的实现不正确:

//example
var result = Rx.Observable.create(function (subscriber) {
   subscriber.next(Math.random());
   subscriber.next(Math.random());
   subscriber.next(Math.random());
   subscriber.complete();
});

//fix, you should also add proper error handling see subscriber.error(..)
load() {
  return Observable.create(subscriber => {
    this.http.get('http://url.xml')
      .map(res => res.text())
      .subscribe(data => {
        if(data) {
          var parser = new DOMParser();
          var xmlData = parser.parseFromString(data, "application/xml");
          var items = xmlData.querySelectorAll("item");
          for (var index = 0; index < items.length; index++) {
            var element = items[index];
            console.log(element);
          }
          subscriber.next(items);
        }
        else
        {
          subscriber.next([]);
        }
        subscriber.complete();
     });
  });
}

10-04 15:25
查看更多