如何在ion-list angular 2 ionic 2

时间:2017-01-13 22:27:49

标签: xml angular typescript ionic-framework

我在控制台中获得了一个<item>数组,每个数组都包含标题,链接,描述,图像和pubDate,但数据不会显示在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>

1 个答案:

答案 0 :(得分:3)

可观察的实施不正确:

//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();
     });
  });
}