ViewChild返回空的nativeElement - Ionic

时间:2017-07-24 02:35:07

标签: html image angular ionic-framework element

我有一个图像元素,我尝试使用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":{}}

它似乎没有返回元素。

1 个答案:

答案 0 :(得分:0)

我必须删除包含图片的ion-item - 然后才有效。它实际上仍然是向控制台登录一个空对象。