【问题标题】:ViewChild returning empty nativeElement - IonicViewChild 返回空的 nativeElement - Ionic
【发布时间】:2017-07-24 02:35:07
【问题描述】:

我有一个图像元素,我正在尝试将ViewChild 用于:

<img class="postimage" #imagey [src]="">

我的控制器是这样的:

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

/**
 * Generated class for the PostpagePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-postpage',
  templateUrl: 'postpage.html',
})
export class PostpagePage {
    @ViewChild('imagey') image:ElementRef;
    imageHolder;

  constructor(public myrenderer: Renderer, public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    this.imageHolder = this.navParams.get("path");
    this.myrenderer.setElementAttribute(this.image.nativeElement, 'src', this.imageHolder);
    console.log(JSON.stringify(this.image));
  }

  pushPage(){
    // push another page on to the navigation stack
    // causing the nav controller to transition to the new page
    // optional data can also be passed to the pushed page.
    //this.navCtrl.push(SignUpPage);
  }

}

ionViewDidLoad 中控制台消息的结果是:

{"nativeElement":{}}

它似乎没有返回一个元素。

【问题讨论】:

    标签: html image angular ionic-framework element


    【解决方案1】:

    我不得不删除包含图像的ion-item - 然后它起作用了。它实际上仍然是在控制台中登录一个空对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 2018-06-15
      • 2017-03-18
      • 2017-03-11
      • 2023-03-21
      相关资源
      最近更新 更多