【问题标题】:Undefined object from my product page我的产品页面中未定义的对象
【发布时间】:2018-08-29 18:09:31
【问题描述】:

这是我在 product-details.ts 中的构造函数

product: any;

  constructor(public navCtrl: NavController, public navParams: NavParams) {


    this.product = this.navParams.get("product");
    console.log(this.product);
    
  }

这是我的家.ts

  openProductPage(product){
    this.navCtrl.push(ProductDetailsPage, {"product": product} );
  }

这是html文件home.html

<ion-list>
    <ion-item *ngFor="let products of moreProducts" text-wrap (click)="openProductPage(product)">
        <ion-thumbnail item-left>
          <img [src]="products.featured_src" />
        </ion-thumbnail>

        <h1>{{products.title}}</h1>
        <p>
          <span [innerHTML]="products.short_description.substr(0, 50) + '...'"></span>
          <span [innerHTML]="products.price_html"></span>
        </p>

         <button ion-button  icon clear item-right>
           <ion-icon name="arrow-forward"></ion-icon>
         </button>
    </ion-item>
  </ion-list>

当我点击一个产品时,它在控制台日志中显示“未定义” 顺便说一下,我在 app.module.ts 中添加了所有必要的东西 其中 import "..." & 声明 & entryComponents

【问题讨论】:

  • 您在 (click) 方法中传递了错误的变量。那应该像 (click)="openProductPage(products)"

标签: javascript angular typescript ionic3


【解决方案1】:

在您的代码中,您在ngFor 中使用了products,因此您应该在调用方法click 时使用该变量

<ion-item *ngFor="let products of moreProducts" text-wrap (click)="openProductPage(products)">

【讨论】:

  • 如果答案对你有用@MohamedAliLassoued,你能接受吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 2018-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多