微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用离子2.

我需要获得htmlelement值.

我实际上使用了viewchild.

这是我的html模板代码

<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值.我访问这个元素.但我没有得到元素的价值.

这是我的组件

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);

      }
    }

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

我尝试了很多方式.

但我得到这个错误

Cannot read property 'nativeElement' of undefined.

解决方法

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

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

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

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

原文地址:https://www.jb51.cc/js/159066.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐