如何解决发送消息后的 NativeScript 空文本字段
html:
<StackLayout class="nt-form chat-wrapper" >
<StackLayout loaded="test" id="chat-form" orientation="horizontal" class="nt-input input-field form">
<TextField
(textChange)="onTextChange($event)"
[text]="inputText"
width="800px"
class="-rounded-lg input"
hint="Nachricht"
style="background-color: #ffffff;"
></TextField>
<button width="120px" class="-rounded-lg fa far fas fab" (tap)="sendMessage();" style="margin-left: 15px;" text=""></button>
</StackLayout>
</StackLayout>
ts:
@Component({
selector: "ItemDetail",styleUrls: ['item-detail.component.css'],templateUrl: "./item-detail.component.html",providers: [DatePipe]
})
export class ItemDetailComponent implements OnInit {
inputText;
constructor(){}
ngOnInit(): void{}
sendMessage(){
if(this.text == undefined){
console.log("Sie haben keine Nachricht eingetippt");
}else{
this.clearText();
this._chatService.addMessage(this.id,this.text,this.me);
}
}
clearText(){
if(this.inputText != ""){
this.inputText = "";
}else{
console.log("Die nachricht ist schon leer")
}
}
}
当我第一次发送消息时,文本被清除。但之后它仍然发送文本但它不会删除文本字段中的消息。
第一次点击:
第二次点击:
它仍然发送消息,但它保留在 TextField 框中
解决方法
我发现始终有效的另一种清除方法是直接设置 TextField
的 text
属性。
<TextField #TextField>
...
@ViewChild('TextField') textField: ElementRef;
clearText(): void {
// keep this line as you are using it for sendMessage()
this.inputText = "";
// clear the textfield
(<TextField>this.textField.nativeElement).text = "";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。