我已经使用NgForm创建了一个表单,并向其中添加了一些控件。该表单用于基本的聊天概念证明。
当用户发送消息时,我需要将消息输入的值重置为空,以便他们可以开始输入下一条消息。但是,我找不到任何有关如何更新控件的值的文档或信息。
我试过很多方法,但都不管用。
下面是我尝试过的一些方法。
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>必须有一种方法来在表单上执行这样的基本操作,比如更新控制器或模型的值,但到目前为止我还没有发现什么。
发布于 2016-05-27 08:44:34
由于某些原因,当您尝试从ControlGroup获取Control时,它希望返回一个AbstractControl类型的值,该值缺少函数updateValue()
如果您将其转换为Control,它应该可以工作。例如:
(<Control>yourControlGroup.controls['some_form_field']).updateValue('new value');在您的例子中,假设formValues的类型是ControlGroup (如果不是,请指定它是什么),下面的代码可能会起作用:
(<Control>formValues.controls['message']).updateValue('')或者也许
(<Control>formValues['message']).updateValue('')https://stackoverflow.com/questions/37469183
复制相似问题