<文本区域>添加 formControlName <textarea> 时默认内容消失元素 [英] <textarea> default content disappears when adding formControlName the <textarea> element
问题描述
我正在尝试在 Angular 4.0.2 中创建一个响应式表单,它有一个 <textarea>
字段,其中包含从数据库中提取的默认内容. 中的内容显示没有任何问题,但是当我将
formControlName="sectionContent"
添加到 时> 元素,它的内容消失了.
I'm trying to create a Reactive Form in Angular 4.0.2, which has a <textarea>
field with default content, pulled from the database. The content in the <textarea>
is showing without any issues, but when I add the formControlName="sectionContent"
to the <textarea>
element, the content from it disappears.
<textarea formControlName="sectionContent ">{{ section.sectionContent }}</textarea
>
此问题仅发生在 元素上,因为表单中有其他
字段,但这些内容仍在出现.
This issue is only happening with the <textarea>
element, as I have other <input>
fields in the form, but those contents are still appearing.
FormGroup 如下所示:
The FormGroup looks like this:
this.sectionForm = new FormGroup({
sectionTitle: new FormControl(),
sectionContent : new FormControl()
});
有人遇到过这个问题吗?
Did anyone encountered this issue?
推荐答案
改用默认值
this.sectionForm = new FormGroup({
sectionTitle: new FormControl(),
sectionContent : new FormControl(this.section.sectionContent)
});
模板
<textarea formControlName="sectionContent"></textarea>
或使用 setValue/pathValue:
or using setValue/pathValue:
this.sectionForm = new FormGroup({
sectionTitle: new FormControl(),
sectionContent : new FormControl()
});
// after received data
this.sectionForm.patchValue({sectionContent: this.section.sectionContent});
演示:https://plnkr.co/edit/NWgzGdUc9cDkKujPgrl4?p=preview
文档:
https://angular.io/docs/ts/latest/api/forms/index/FormControl-class.htmlhttps://angular.io/docs/ts/latest/api/forms/index/FormGroup-class.html
设置值:
设置 FormGroup 的值.它接受一个匹配的对象组的结构,以控件名称为键.
Sets the value of the FormGroup. It accepts an object that matches the structure of the group, with control names as keys.
这个方法执行严格的检查,所以如果你尝试设置不存在的控件的值,或者如果您排除控件的值.
This method performs strict checks, so it will throw an error if you try to set the value of a control that doesn't exist or if you exclude the value of a control.
补丁值:
修补 FormGroup 的值.它接受一个具有控制权的对象名称作为键,并将尽最大努力将值匹配到正确的组内控制.
Patches the value of the FormGroup. It accepts an object with control names as keys, and will do its best to match the values to the correct controls in the group.
它同时接受组的超集和子集而不抛出一个错误.
It accepts both super-sets and sub-sets of the group without throwing an error.
这篇关于<文本区域>添加 formControlName <textarea> 时默认内容消失元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!