Ani*_*shu 13 typescript ionic2 angular
如何使用离子2获取html元素值
在我的HTML代码下面
<div class="messagesholder" *ngFor="let chat of chatval | orderby:'[date]'" >
<div *ngIf="chat.sender == currentuser || chat.receiver == currentuser">
<div *ngIf="chat.date" style="text-align: center;" >
<p style="font-size:9px;" id="amount" #amount>{{chat.date | amDateFormat:'LL'}}</p>
<input #myname [ngModel]="range" (ngModelChange)="saverange($event)"/>
<input #myname type="text" value={{chat.date}}>
</div>
</div>
<div class="message" *ngIf="chat.sender == currentuser || chat.receiver == currentuser" [ngClass]="{'me': currentuser == chat.sender}">
<div class='image' *ngIf="chat.path" >
<img *ngIf="chat.path" [src]="chat.path"/><br>
<span *ngIf="chat.path_text">{{chat.path_text}}</span>
<span style="font-size:9px;">{{chat.date | amDateFormat:'hh:mmA'}}</span>
</div>
<div *ngIf="chat.message_text">
<span>{{chat.message_text}}</span>
<span style="font-size:9px;">{{chat.date | amDateFormat:'hh:mmA'}}</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在我的ts文件下面
import { Component,Inject,ViewChild,ElementRef,AfterViewInit} from '@angular/core';
export class ChatPage implements AfterViewInit {
@ViewChild('myname') input:ElementRef;
constructor(public modalCtrl: ModalController,public navCtrl: NavController) {}
ngAfterViewInit() {
console.log(this.input.nativeElement.value);
}
}
Run Code Online (Sandbox Code Playgroud)
重复相同的日期值.我希望不重复相同的日期值.
因为我会检查两个变量.
所以我需要chat.date值.因为我绑定了input的值.但是我无法获得input元素的值.
我收到这个错误
无法读取未定义的属性"nativeElement"
如何解决这个问题.或任何其他方式来找到slutions.
谢谢
Hun*_*Cao 21
我创建了一个plnkr链接:https://plnkr.co/edit/49TEP8lB4lNJEKsy3IDq ? p = preview
它对我有用,所以你可能想创建自己的plnkr,所以ppl可以提供帮助
export class ApiDemoApp{
root = ApiDemoPage;
@ViewChild('myname') input:ElementRef;
constructor() {
}
ngAfterViewInit() {
console.log(this.input.nativeElement.value);
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码
@ViewChild('myname') input:any;
ngAfterViewInit() {
console.log(this.input.nativeElement.value) ;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64487 次 |
| 最近记录: |