如何使用forncontrolname在Textarea中修复字符串插值?(Angular)
在以下代码中,如果我使用
formControlname =“ work_sample_description”
textarea,文本区域不加载值
worksampleambj.work_sample_description
如果我删除
formControlname =“ work_sample_description”
代码按预期运行..!
如何修复它?
<form [formGroup]="workSampleEditForm" (ngSubmit)="edit_work_sample(workSampleEditForm.value)">
<mat-card-content>
<mat-form-field appearance="fill" class="full-width">
<textarea matInput rows="7" formControlName="work_sample_description">{{WorkSampleObj.work_sample_description}}</textarea>
</mat-form-field>
</mat-card-content>
<mat-card-actions>
<button mat-button *ngIf="workSampleEditForm.disabled" (click)="workSampleEditForm.enable()">Edit description</button>
<button mat-button *ngIf="workSampleEditForm.enabled">Save Changes</button>
<button mat-button>Delete</button>
</mat-card-actions>
</form>
我也尝试了
workSampleEditForm = new FormGroup({
work_sample_description : new FormControl(this.WorkSampleObj.work_sample_description,[])
});
---------------
<textarea matInput rows="7" [formControlName]="'work_sample_description'"></textarea>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,
[formControlname]
期望给定表单控件的字符串名称。您正在传递它。如果您形成组worksampleameeditform
包含work_sample_description
formcontrol
this.workSampleObj.work_sample_description
的值,而不是创建表单时,应使用setValue
进行设置。请参阅更新 。
The issue is, that
[formControlName]
expects string name of the given form control. And you are passing reference to it. If you form groupworkSampleEditForm
containswork_sample_description
form control then just pass its name as a string (put the name in the single quotes)As mentioned by @MikeOne, if you get the value of
this.WorkSampleObj.work_sample_description
from API and not when the Form is created, you should set it usingsetValue
.See updated stackblitz.
[(ngmodel)]解决我的问题。
[(ngModel)] solve my issue.