简体   繁体   English

动态创建的类型复选框的输入未通过NgModel正确绑定值

[英]Dynamically created input of type checkbox doesn't properly bind value through NgModel

I've got a dynamically created form, wrapped in a table, that appears when I click in an "edit" button of that same row. 我有一个动态创建的表格,包装在表格中,当我单击同一行的“编辑”按钮时,该表格就会出现。 There are a lot of conditionals inside that dynamic table that when the row is being edited, they show some inputs. 该动态表中有很多条件,当编辑该行时,它们显示一些输入。

The type, and the binding of those inputs, are all dynamic. 这些输入的类型和绑定都是动态的。 Let's check this one: 让我们检查一下这个:

<td  *ngIf="table?.getInputType(column.key) && table?.isInputActive(column.key, rowIndex) && column.key != table?.buttonsColumn">
    <input *ngIf="table?.getInputType(column.key) != 'select' && column.key != table?.buttonsColumn"
        [type]="table?.getInputType(column.key)"
        [(ngModel)]="sortedValue.referenceObject[column.key]">

This binding works perfect for both selects (which are not included in this snippet), and text fields. 此绑定对于选择(不包含在此代码段中)和文本字段都是完美的。 But it doesn't bind correctly for checkbox inputs. 但是它不能正确绑定复选框输入。 It doesn't really get the actual value inside the given object, therefore the checkbox is always as "false" (though the value is at "true" sometimes). 它并没有真正获得给定对象内的实际值,因此该复选框始终为“ false”(尽管有时该值为“ true”)。 Consequently, ticking the box and saving the result won't generate any change. 因此,勾选该框并保存结果不会产生任何变化。

The reference you can see inside the NgModel is perfectly done; 您可以在NgModel中看到的引用已经完成; I already checked it and the names involved in this key-value object are correctly put. 我已经检查过了,并且正确放置了此键值对象中涉及的名称。 The problem is somewhere else, but I don't know where. 问题出在别的地方,但我不知道在哪里。

Any help? 有什么帮助吗?

This case is discussed in the issue 7329 on GitHub, and is considered "a limitation of the framework" (the issue is closed). 该案例在GitHub上的问题7329中进行了讨论,被认为是“框架的局限性”(此问题已结案)。 One workaround mentioned in the discussion is to use conditional directives. 讨论中提到的一种解决方法是使用条件指令。 See this stackblitz for a demo. 有关演示,请参见此堆叠闪电战

<ng-container [ngSwitch]="inputType">
  <input *ngSwitchCase="'checkbox'" type="checkbox" [(ngModel)]="value">
  <input *ngSwitchCase="'password'" type="password" [(ngModel)]="value">
  <input *ngSwitchCase="'email'" type="email" [(ngModel)]="value">
  <input *ngSwitchDefault type="text" [(ngModel)]="value">
</ng-container>

or 要么

<input *ngIf="inputType === 'checkbox'" type="checkbox" [(ngModel)]="value">
<input *ngIf="inputType !== 'checkbox'" type="text" [(ngModel)]="value">

You can try giving an 'id' to each checkbox. 您可以尝试为每个复选框指定一个“ id”。 I have used rowIndex as id in the below example. 在以下示例中,我已使用rowIndex作为id。 You can use it or give some other unique value as id. 您可以使用它,也可以提供其他一些唯一值作为id。

<td  *ngIf="table?.getInputType(column.key) && table?.isInputActive(column.key, rowIndex) && column.key != table?.buttonsColumn">
   <input *ngIf="table?.getInputType(column.key) != 'select' && column.key != table?.buttonsColumn" 
                     [id]="rowIndex" [type]="table?.getInputType(column.key)"
                            [(ngModel)]="sortedValue.referenceObject[column.key]">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM