英文:
dynamic formcontrol in reactive forms - angular
问题
我在组件的 .ts
文件中有一个动态的 formControl
,其中 addControl
的使用如下:
//...
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));
}
...///
我的组件的 .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>
显示部分工作正常。但是 <span>
标签中的计数是实时计算的,根据输入的文本在界面中显示。
例如,初始时显示 "25 of 500 characters",随着继续输入,计数器应该逐渐增加。我在静态表单中实现了这一点,代码如下:
<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>
但是同样的概念在动态表单中不起作用。{{e.boardAnswers.length}}
只在页面加载时给出值,但是在文本区域中输入文本时并不会增加。如何在动态表单中处理这个问题?请提供建议。
英文:
I have a dynamic formControl where addControl is as follows in my component.ts file:
//...
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:
<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.
like for example shows 25 of 500 characters and as you keep typing the counter has to increase. 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.
How do I handle the same with dynamic forms. Suggestions please?
答案1
得分: 1
以下是翻译好的内容:
工作脚本:
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));
}
}
更好的方法是为计算长度创建一个方法:
getStrLength(key: string | number): number {
return this.afterActionReportForm.get(key.toString()).value?.length || 0;
}
<span> {{getStrLength(e.quesId)}} of 500 characters</span>
英文:
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>
答案2
得分: 1
我有同样的需求。我使用了getter方法 this.f
来获取表单控件。将当前的 quesId
与控件对象中的ID进行比较,以获取值和长度。这对我有效。
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
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;
}
如果您有任何其他问题,请随时提出。
英文:
I had same requirement. I used the getter method this.f
to fetch the form controls. Compared the current quesId with the id from the object of controls to get the value and length. This works for me
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
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论