2016-05-26 32 views
1

我已經使用NgForm創建了一個表單併爲其添加了一些控件。該表單用於概念的基本聊天證明。Angular2 RC1更新controlGroup中的控制值

當用戶發送消息時,我需要將消息輸入的值重置爲空,以便他們開始輸入下一條消息。但是,我無法找到任何有關如何更新控件價值的文檔或信息。

我已經嘗試了各種東西,但沒有什麼工作......

這裏的一些事情,我已經試過。

sendMessage(formValues) { 
    formValues.message = ''; // This works but the view doesn't update so there is evidently no binding or observers attached. 
    formValues.message.value = ''; // This throws an error that type string doesn't value defined 
    formValues.message.val(''); // This throws an error that type string doesn't contain a method val() 
    formValues['message'] = '' // This works but the view doesn't get updated so there is evidently no binding/observers here. 
    formValues.message.updateValue(''); // This also throws an error that type string doesn't have a updateValue() method. 
} 

繼承人的視圖模板:

<form name='chat-form' (ngSubmit)='sendMessage(messageForm.value)' #messageForm='ngForm'> 
    <input class='message-body' ngControl='message' placeholder='Enter your message'/> 
    <button type='submit'>Send</button> 
</form> 

必須有辦法做到在窗體上這樣的基本技術動作如更新從控制器或模型中的價值,但到目前爲止,我已經幹了。

+0

formValues被一個ControlGroup? – Blacksonic

+0

是的,我已經添加了上面的視圖模板代碼,以顯示如何通過ngForm指令創建ControlGroup – efarley

回答

2

出於某種原因,ControlGroup,當您試圖從它那裏得到一個Control,想要回AbstractControl類型如果你把它轉換爲Control它應該工作缺乏功能updateValue()

的值。例如

(<Control>yourControlGroup.controls['some_form_field']).updateValue('new value'); 

在你的情況,也許下面將工作假設formValues是類型ControlGroup的(如果不是請註明它是什麼):

(<Control>formValues.controls['message']).updateValue('') 

或許

(<Control>formValues['message']).updateValue('')