2017-06-01 52 views
2

我有一個聊天應用程序客戶端,我想清除發送按鈕後的文本區域。角度2清除表單值拋出只讀異常

代碼如下:

<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']值= '' 我得到只讀例外。

回答

2

使用setValue()功能改變FormControl值:

this.chatForm.controls['messageInput'].setValue('');