【发布时间】:2017-09-09 01:33:29
【问题描述】:
我正在使用离子 2。
我需要获取 HTML 元素值。
其实我用的是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.
【问题讨论】:
-
您的
<p>在*ngIf中吗?你能在 Plunker 中重现吗?根据您的问题,它应该可以正常工作。value应该返回什么?<p>元素没有value属性。 -
谢谢。我需要原生元素的访问值
-
你没有回答我的任何问题,原生元素没有价值。
-
其实我只是控制台this.abc.nativeElement。我得到这个错误无法读取未定义的属性'nativeElement'。为什么?
-
@Günter Zöchbauer
没有 *ngIf。我控制台 this.abc 的值它在 innerhtml 等....但同时我控制台 this.abc.nativeelement。我无法访问
标签: angular typescript ionic2