无法读取未定义的属性"native-element"

无法读取未定义的属性

问题描述:

我正在使用离子2.

我需要获取HTML元素值.

I need to get HTML element value.

实际上,我使用了viewchild.

Actually, I used viewchild.

这是我的html模板代码

Here is my html template code

<div class="messagesholder" *ngFor="let chat of chatval | orderby:'[date]'; let last = last">
       {{last ? callFunction() : ''}} 

   <div *ngIf="chat.sender == currentuser || chat.receiver == currentuser">    
     <p class="chat-date"  id="abc" #abc>{{chat.date | amDateFormat:'LL'}}</p>                 
              {{checkdate()}}                         
   </div>

chat.date值是Firebase值.我访问此元素.但是我没有得到元素的值.

chat.date value is firebase value. I access this element. But I didn't get the value of the element.

这是我的组成部分

import {Component, ElementRef,ViewChild, OnInit} from '@angular/core';

    export class ChatPage   {
      @ViewChild(Content) content: Content;
      @ViewChild('abc')abc: ElementRef;
       constructor(){}

      ngAfterViewInit(){

       console.log("afterinit");
       console.log(this.abc.nativeElement.value);

      }
    }

我引用了此链接如何我可以在组件模板中选择一个元素吗?

我尝试了很多方法.

但是我收到此错误

Cannot read property 'nativeElement' of undefined.

我认为您正在尝试在完全渲染之前从html获取值.如果您尝试通过单击按钮来打印该值,那么它将起作用.

I think you are tring to get the value from html before rendering completely. If you try to print the value in a button click, it will works.

根据您的代码,我做了一些修改.请尝试以下操作,它对我有用.

depend on your code I have modified a little.Try the below, it is working for me.

  ngAfterViewInit() {
    console.log("afterinit");
    setTimeout(() => {
      console.log(this.abc.nativeElement.innerText);
    }, 1000);
  }

注意:如果不起作用,请增加超时时间,然后重试.

Note: If not working, please increase the timeout time and try again.