简体   繁体   English

单击“离子项目”后无法显示提要数据-Ionic 2

[英]Unable to show feed data after clicking ion-item - Ionic 2

When I click on an ion-item of my list in my home page I'm not getting the data of the feed in the new page (DetailPage). 当我在主页上单击列表中的一个ion-item ,我没有在新页面(DetailPage)中获取提要的数据。 I can't sort out what I'm missing, could you help me? 我无法弄清我所缺少的,您能帮我吗?

Stack: 堆:

Runtime Error
Error in ./DetailPage class DetailPage - caused by: Cannot read property 'title' of undefined

This is my home.html - home.ts 这是我的home.html - home.ts

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

<ion-content padding>
    <ion-list>
        <ion-list inset>
            <ion-item *ngFor="let entry of entries" (click)="openPage(entry)">{{entry.title}}</ion-item>
    </ion-list>
    </ion-list>
</ion-content>

|--------------------------------------------------------|

import { NavController } from 'ionic-angular';
import { Component } from '@angular/core';
import { RssService } from '../../providers/rss-service/rss-service';
import { DetailPage } from '../detail-page/detail-page';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [RssService]
})
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});
  }

}

detail-page.html - detail-page.ts detail-page.html - detail-page.ts

<ion-header>
  <ion-navbar color="primary">
    <ion-title>{{entry.title}}</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
    <!--<div [innerHTML]="entry.description"></div>-->
</ion-content>

|--------------------------------------------------|

import {NavController, NavParams} from 'ionic-angular';
import { Component } from '@angular/core';

@Component({
    templateUrl: 'detail-page.html'
})
export class DetailPage {

    constructor(public nav: NavController, navParams:NavParams) {
        console.log('run');
        this.nav = nav;
        var entry = navParams.get('selectedEntry');
        console.log('my entry is '+ entry.title);
    }
}

I think it's because entry is not scoped within the class. 我认为这是因为条目不在类内。 It's only scoped within the constructor. 它仅在构造函数中作用域。 Can you try this code instead for DetailPage? 您可以代替DetailPage尝试使用此代码吗?

import {NavController, NavParams} from 'ionic-angular';
import { Component } from '@angular/core';

@Component({
    templateUrl: 'detail-page.html'
})
export class DetailPage {
    entry:any = {};
    constructor(public nav: NavController, navParams:NavParams) {
        console.log('run');
        this.nav = nav;
        this.entry = navParams.get('selectedEntry');
        console.log('my entry is '+ this.entry.title);
    }
}

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

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