【发布时间】:2019-03-21 16:11:29
【问题描述】:
我有以下问题-
组件 A:
<!-- LIST OF URLs-->
<exl-file-list
*ngIf="hasLinks()"
listTitle="Added links:"
[expandable]="editableFiles"
expandAllButton="Edit metadata"
(edit)="onLinkEdit($event)"
(expandAll)="allLinksEditable = !allLinksEditable"
(removeAll)="onRemoveAllLinks()">
<!-- URL items -->
<exl-file-list-item
*ngFor="let link of depositFormDataService.mainForm.value.links let index = index"
[item]="link"
[index]="index"
(remove)="onLinkRemove($event)"
(edit)="onLinkEdit($event, index)">
<!-- metadata of each URL -->
**<esp-deposit-link-metadata
[index]="index">
</esp-deposit-link-metadata>**
</exl-file-list-item>
</exl-file-list>
组件 B-esp-deposit-link-metadata:
<div class="metadata-container">
<mat-form-field class="hasnt-underline">
<mat-label>Description</mat-label>
<textarea
matInput
[(ngModel)]="description"
**(ngModelChange)="onChangeDescription()"
** #textarea placeholder="Describe the link"
matTextareaAutosize></textarea>
</mat-form-field>
</div>
onChangeDescription 方法,在depositFormDataService.mainForm.links 中更新了我的formGroup
onChangeDescription(){
this.depositFormDataService
.updateLinkDescription(this.index,this.description);
}
内容:
updateLinkDescription(index, description){
const link = this.links.at(index) as FormGroup;
link.setControl('description', new FormControl(description));
}
depositFormDataService.mainForm 将链接保存为 FormArray。 链接是一个具有三个 formControls 的对象,其中一个是“描述”。
每次onChangeDescription()被调用,exl-file-list-item和esp-deposit-link-metadata的constructors被调用,所有视图都被刷新,我没有理由。
【问题讨论】:
-
谢谢!关于您之前给我的“[(ngModel)]="description" (ngModelChange)="onChangeDescription()" 评论 - 如果我写 [ngModel],描述字段根本不会更新..
标签: javascript angular typescript angular-reactive-forms