javascript - 错误错误 : Uncaught (in promise): TypeError: Cannot read property 'title' of undefined

标签 javascript angular ionic2

<分区>

我想在这两个页面之间传递数据。可以从新的详情页面加载到新的详情页面,但这里遇到的问题是无法从详情页面获取 api 数据。

news.html新页面

<ion-header>
  <ion-navbar color="danger" no-border-bottom>
    <button ion-button menuToggle>
      <ion-icon name="ios-contact"></ion-icon>
    </button>

    <ion-title>民安</ion-title>

  </ion-navbar>

</ion-header>

<ion-content padding>

      <ion-item-group *ngFor="let new of news">

          <ion-thumbnail *ngIf="new.Preview_image1" item-start>
            <img src="{{new.Preview_image1}}">
          </ion-thumbnail>
          <button ion-item (click)="goToNewsDetail(new)">
            <h2>{{new.title}}</h2>
            <h2>{{new.publish_time}}</h2>
        <p>
            <ion-icon name="chatboxes">{{new.comments_count}}</ion-icon>
        </p>
        </button>


      </ion-item-group>

</ion-content>

news.ts新功能页面

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

import { NewsDataProvider } from '../../providers/news-data/news-data';
import { NewsDetailPage } from '../news-detail/news-detail';

@IonicPage()
@Component({
  selector: 'page-news',
  templateUrl: 'news.html',
})
export class NewsPage {
    news:any;
  constructor(public navCtrl: NavController, public navParams: NavParams,public newsData:NewsDataProvider){
    this.getNews();
  }

  getNews() {
      this.newsData.getNews().then(data => {
        this.news = data;
      });
  }

  goToNewsDetail() {
    this.navCtrl.push(NewsDetailPage);
  }

}

新闻细节.ts 。新闻详情功能页

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { NewsDataProvider } from '../../providers/news-data/news-data';

@IonicPage()
@Component({
  selector: 'page-news-detail',
  templateUrl: 'news-detail.html',
})
export class NewsDetailPage {
    new: any;
    constructor(public navCtrl: NavController, public navParams: NavParams,public newsData:NewsDataProvider) {
        this.new = navParams.get('new');
  }

}

news-detail.html 新闻详情页

<ion-header>

  <ion-navbar color="danger">
    <ion-title>{{new.title}}</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
    news content
</ion-content>

news.data.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class NewsDataProvider {
    data:any;

  constructor(public http: Http) {
    //console.log('Hello NewsDataProvider Provider');
  }

  getNews() {

    if (this.data) {
      return Promise.resolve(this.data);
    }
    return new Promise(resolve => {
      this.http.get('http://servertrj.com/api/news?api_key=123').map(res => res.json().data).subscribe(data => {
          this.data = data;
          resolve(this.data);
        });
    });
  }

}

最佳答案

在这里你通过你的对象发送<button ion-item (click)="goToNewsDetail(new)">但您的方法不接受它或将其发送到新页面。

goToNewsDetail(newsItem:any) {
  this.navCtrl.push(NewsDetailPage, { new: newsItem });
}

您现在可以在 NewsDetailPage 中接受它就像你已经有了 this.new = navParams.get('new');

您可能还需要向页面添加一个安全操作符,如前所述{{new?.Title}}

在不相关的说明中,您可能想要更改您的 (click)(tap) . (click)事件为单击添加了 300 毫秒的延迟以允许双击,其中 (tap)事件就像一个普通的移动水龙头。当您的应用程序在移动设备上时,这还将消除任何不必要的意外点击。

关于javascript - 错误错误 : Uncaught (in promise): TypeError: Cannot read property 'title' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275818/

相关文章:

angular - Angular2 上的 .map/.subscribe 如何工作?

angular - 重构 switch 语句以降低代码复杂度

ionic-framework - Ionic 以编程方式显示 ion-refresher

angular - Ionic App,Typescript 错误找不到名称 'RequestMode'

ionic-framework - Ionic Native + IOS + OneSignal

javascript - 如何检测对象是否在特定位置休眠?

javascript - 线程 "main"org.openqa.selenium.WebDriverException : unterminated string literal 中出现异常

html - 如何设置 Angular ng-template 选择器的样式

javascript - 使文本不可选择

javascript - 如何使用 Javascript 在 UIWebview 上添加填充