如何使用forncontrolname在Textarea中修复字符串插值?(Angular)

发布于 2025-01-27 01:58:28 字数 1441 浏览 3 评论 0 原文

在以下代码中,如果我使用

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>

in the following code if I use

formControlName="work_sample_description"

for textarea , the text area does not load with the value of

WorkSampleObj.work_sample_description

if I remove

formControlName="work_sample_description"

code runs as expected..!

How to fix it?

<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>

I tried also this

workSampleEditForm = new FormGroup({
    work_sample_description : new FormControl(this.WorkSampleObj.work_sample_description,[])
  });

---------------
<textarea matInput rows="7" [formControlName]="'work_sample_description'"></textarea>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2025-02-03 01:58:28

问题是, [formControlname] 期望给定表单控件的字符串名称。您正在传递它。如果您形成组 worksampleameeditform 包含 work_sample_description form

<textarea 
  matInput rows="7" 
  [formControlName]="'work_sample_description'">
</textarea>

control this.workSampleObj.work_sample_description 的值,而不是创建表单时,应使用 setValue 进行设置。

ngOnInit(): void {
  this.someApiCall.subscribe((res) => {
    this.testForm.get('textAreaFC').setValue(res.value);
  });
}

请参阅更新

The issue is, that [formControlName] expects string name of the given form control. And you are passing reference to it. If you form group workSampleEditForm contains work_sample_description form control then just pass its name as a string (put the name in the single quotes)

<textarea 
  matInput rows="7" 
  [formControlName]="'work_sample_description'">
</textarea>

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 using setValue.

ngOnInit(): void {
  this.someApiCall.subscribe((res) => {
    this.testForm.get('textAreaFC').setValue(res.value);
  });
}

See updated stackblitz.

帅冕 2025-02-03 01:58:28

[(ngmodel)]解决我的问题。

<textarea matInput rows="7" formControlName="work_sample_description" 
[(ngModel)]="WorkSampleObj.work_sample_description"></textarea>

[(ngModel)] solve my issue.

<textarea matInput rows="7" formControlName="work_sample_description" 
[(ngModel)]="WorkSampleObj.work_sample_description"></textarea>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文