我有一个聊天应用程序客户端,我想在点击发送按钮后清除文本区域。
代码如下:
<form [formGroup]="chatForm" (ngSubmit)="sendChatMessage()">
<ion-input type="text" formControlName="messageInput" placeholder="start typing..."></ion-input>
<ion-buttons end>
<button item-right ion-button clear type="submit" [disabled]="chatForm.controls['messageInput'].value === ''"><ion-icon name="ios-send" style="zoom:2.0;"></ion-icon></button>
</ion-buttons>
</form>
但是,在 sendChatMessage() 中,如果我尝试这样做 chatForm.controls['messageInput'].value='' 我收到只读异常。
最佳答案
使用setValue()
函数更改FormControl
的值:
this.chatForm.controls['messageInput'].setValue('');
关于Angular 2清除表单值抛出只读异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44317830/