javascript – 无法读取未定义的属性“nativeElement”

前端之家收集整理的这篇文章主要介绍了javascript – 无法读取未定义的属性“nativeElement”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用离子2.

我需要获得htmlelement值.

我实际上使用了viewchild.

这是我的html模板代码

  1. <div class="messagesholder" *ngFor="let chat of chatval | orderby:'[date]'; let last = last">
  2. {{last ? callFunction() : ''}}
  3.  
  4. <div *ngIf="chat.sender == currentuser || chat.receiver == currentuser">
  5. <p class="chat-date" id="abc" #abc>{{chat.date | amDateFormat:'LL'}}</p>
  6. {{checkdate()}}
  7. </div>

chat.date值是firebase值.我访问这个元素.但我没有得到元素的价值.

这是我的组件

  1. import {Component,ElementRef,ViewChild,OnInit} from '@angular/core';
  2.  
  3. export class ChatPage {
  4. @ViewChild(Content) content: Content;
  5. @ViewChild('abc')abc: ElementRef;
  6. constructor(){}
  7.  
  8. ngAfterViewInit(){
  9.  
  10. console.log("afterinit");
  11. console.log(this.abc.nativeElement.value);
  12.  
  13. }
  14. }

我把这个链接称为How can I select an element in a component template?

我尝试了很多方式.

但我得到这个错误

  1. Cannot read property 'nativeElement' of undefined.

解决方法

我认为你想在完全渲染之前从html中获取值.如果您尝试在按钮单击中打印该值,它将起作用.

取决于你的代码我已经修改了一点.试试下面,它对我有用.

  1. ngAfterViewInit() {
  2. console.log("afterinit");
  3. setTimeout(() => {
  4. console.log(this.abc.nativeElement.innerText);
  5. },1000);
  6. }

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

猜你在找的JavaScript相关文章