我有以下代碼
(this.formGroups.get('items') as FormArray).controls.forEach((item, index, object) => {
console.log(item.value.attributeDisplayName);
if (item.value.attributeDisplayName === '') {
object.splice(index, 1);
}
});
我不確定代碼是否正確,但是它沒有被執行。基本上我需要從我的formGroups陣列中洗掉所有物件(s)/formGroup(s),其中attributeDisplayName為空。
item.value給我當前物件的屬性attributeDisplayName所在。請建議。謝謝。
uj5u.com熱心網友回復:
'removeAt' 是從 FormArray 中洗掉單個物件/控制元件的正確方法,但是如果您在回圈中將它與多個控制元件一起使用,則會導致洗掉一些而不是其他控制元件。
這可能是由于從您正在回圈的同一索引中洗掉而發生的,為了克服這個問題,您可以首先識別不需要的控制元件,然后反向洗掉它們。
Stackblitz 示例
deleteEmpty() {
let indexToRemove: number[] = [];
let fromArray = this.form.get('items') as FormArray;
fromArray.controls.forEach((control, index) => {
if (!control.value.attributeDisplayName) {
indexToRemove.push(index);
}
});
indexToRemove.reverse().forEach((index) => {
fromArray.removeAt(index);
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/385199.html
標籤:有角的
上一篇:AppSynconUpdate訂閱不是由python請求POST到GraphQL端點觸發的
下一篇:linux編譯安裝nginx
