[英]Angular 9 Reactive Forms: checkbox not getting updated when using trackBy
I am building the Products form using reactive forms .我正在使用反应式表单构建产品表单。
When I select all the checkboxes and click reset, the checkboxes are not getting updated**当我选择所有复选框并单击重置时,复选框没有更新**
When I remove trackBy it is working fine, how can I make this work using ngFor trackBy
?当我删除 trackBy 时它工作正常,如何使用ngFor trackBy
使其工作?
here is the example : stackblitz Products Form这是示例: stackblitz 产品表单
It is because you are returning index in trackBy
function, the first argument of trackBy
function is index of the item and second is the item itself you suppose to return the second argument.这是因为您在返回指数trackBy
功能,第一个参数trackBy
功能是该项目的指标,第二个是你想返回第二个参数的项目本身。
In your example it would look like something.在您的示例中,它看起来像一些东西。
PS I renamed trackBy function with a generic name. PS 我用通用名称重命名了 trackBy 函数。
trackByFn(_, item){
return item;
}
I forked your stackblitz you can check that out as well.我分叉了你的stackblitz,你也可以检查一下。
It's because your trackByFn
function is not correct.这是因为您的trackByFn
函数不正确。
The
trackBy
function takes the index and the current item as arguments and needs to return the unique identifier for this item.trackBy
函数将索引和当前项作为参数,并需要返回该项的唯一标识符。
So, try this:所以,试试这个:
trackByFn(index, item){
return index;
}
You can check the bug fixed version here on Stackblitz .您可以在Stackblitz上查看错误修复版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.