简体   繁体   English

反应形式中的动态表单控制 - 角度

[英]dynamic formcontrol in reactive forms - angular

I have a dynamic formControl where addControl is as follows in my component.ts file:我有一个动态 formControl,其中 addControl 在我的 component.ts 文件中如下所示:

//...
this.reportList = this.data.reportList; 
for (let i = 0; i < this.reportList.length; i++) {
    const key = this.reportList[i].quesId+'';
    this.afterActionReportForm.addControl(key, new FormControl(this.reportList[i].boardAnswers));
}
...///

my component.html file code looks as follows:我的 component.html 文件代码如下所示:

<div  class="row" *ngIf="reportList && reportList.length > 0" style="padding:10px;">
    <div *ngFor="let e of reportList; let i = index" class="col-sm-6" style="margin-top: 2em;">
        {{i+1}}. {{e.question}}
        <textarea style="width:100%" #{{e.quesId}} rows="8" cols="75" maxlength="500" formControlName="{{e.quesId}}"
        class="form-control"  pInputTextArea ></textarea>
        <span> {{e.boardAnswers.length}} of 500 characters</span>
    </div>
</div>

The display part is working fine.显示部分工作正常。 But the span has a count which id calculated on the fly and shows up in the UI based on what the textArea is inputed.但是跨度有一个计数,该计数是动态计算的,并根据输入的 textArea 显示在 UI 中。

like for example shows 25 of 500 characters and as you keep typing the counter has to increase.例如,显示 500 个字符中的 25 个,并且随着您不断输入,计数器必须增加。 I was able to do that for my static form as follows: This one works in static form:我能够为我的静态形式做到这一点,如下所示:这个以静态形式工作:

<div class="col-sm-10">
    <textarea style="width:80%" #message rows="8" cols="75" maxlength="500" 
    formControlName="message" class="form-control"  pInputTextArea ></textarea>
    <span> {{message.value?.length || 0}} of 500 characters</span>
</div>

But the same concept doesnt work for dynamic form.{{e.boardAnswers.length}} gives me value only when the page loads up, however when i type in the textArea it doesnt increment.但是相同的概念不适用于动态表单。{{e.boardAnswers.length}} 仅在页面加载时才给我值,但是当我输入 textArea 时,它不会增加。 How do I handle the same with dynamic forms.我如何处理动态表单。 Suggestions please?请提出建议?

It's working script:这是工作脚本:

app.component.html
<form [formGroup]="afterActionReportForm">
  <div  class="row" *ngIf="reportList && reportList.length > 0" style="padding:10px;">
    <div *ngFor="let e of reportList; let i = index" class="col-sm-6" style="margin-top: 2em;">
        {{i+1}}. {{e.question}}
        <textarea style="width:100%" #{{e.quesId}} rows="8" cols="75" maxlength="500" [formControlName]="e.quesId"
        class="form-control"  pInputTextArea ></textarea>
        <span> {{afterActionReportForm.get(e.quesId.toString()).value.length}} of 500 characters</span>
    </div>
</div>
</form>

app.component.ts

  ngOnInit() {
    this.afterActionReportForm = new FormGroup({});
    this.reportList = this.data.reportList;
    for (let i = 0; i < this.reportList.length; i++) {
        const key = this.reportList[i].quesId+'';
        this.afterActionReportForm.addControl(key, new FormControl(this.reportList[i].boardAnswers));
    }
  }

And more better create method for counting length更好地创建计数长度的方法

getStrLength(key: string | number): number {
    return this.afterActionReportForm.get(key.toString()).value?.length || 0;
  }

<span> {{getStrLength(e.quesId)}} of 500 characters</span>

I had same requirement.我有同样的要求。 I used the getter method this.f to fetch the form controls.我使用 getter 方法this.f来获取表单控件。 Compared the current quesId with the id from the object of controls to get the value and length.将当前 quesId 与来自控件对象的 id 进行比较以获取值和长度。 This works for me这对我有用

Html html

    <form [formGroup]="afterActionReportForm">
    <div  class="row" *ngIf="reportList && reportList.length > 0" style="padding:10px;">
      <div *ngFor="let e of reportList; let i = index" class="col-sm-6" style="margin-top: 2em;">
          {{i+1}}. {{e.question}}
          <textarea style="width:100%" #{{e.quesId}} rows="8" cols="75" maxlength="500"  [formControlName]="e.quesId"
          class="form-control"  pInputTextArea ></textarea>
          <span > {{getMyLength(e.quesId)}} of 500 characters</span>
      </div>
  </div>
  </form>

TS TS

 get f() { return this.afterActionReportForm.controls; }
 

   getMyLength(id){
    var len;
  Object.entries(this.f).forEach(([key,value]) => {
    if(key === id){
     len= value.value.length;
    }
  });
  return len;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM