简体   繁体   English

Angular 9 Reactive Forms:使用 trackBy 时复选框未更新

[英]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.

相关问题 从 Reactive Forms Angular 的输入字段获取更新的值 - Getting Updated Value from Input Field from Reactive Forms Angular Angular 反应式 forms 复选框验证 - Angular reactive forms checkbox validations Angular 2 Reactive Forms复选框状态 - Angular 2 Reactive Forms checkbox state 在带有反应形式的角度v4中使用禁用状态时,无法将复选框值设置为false - Unable to set checkbox value to false when using disabled state in angular v4 with reactive forms 使用反应式 forms ANGULAR 8 中的验证选中或取消选中复选框时禁用/启用输入字段 - disable/enable input filed when checkbox is checked or unchecked using validations in reactive forms ANGULAR 8 选中复选框时,角反应式表单将禁用输入 - Angular reactive forms disable input when checkbox checked 如何使用angular8中的反应形式选择和取消选择复选框 - how to select and deselect checkbox using reactive forms in angular8 Angular 4-反应形式-从列表中未引用的对象中选择列表中的项目-Trackby问题? - Angular 4 - Reactive Forms - select item in a list from object not referenced in this list - trackby issue? 输入字段未在Reactive Forms Angular 4中更新 - Input field not updated in Reactive Forms Angular 4 @ syncfusion / ej2-angular-calendars datepicker与反应形式一起使用时获取以前的值 - @syncfusion/ej2-angular-calendars datepicker getting previous values when using with reactive forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM