[英]Can't bind to 'ngValue' since it isn't a known property of 'input'
I have imported FormsModule and ReactiveFormsModule in my app.module.ts
file.我在
app.module.ts
文件中导入了 FormsModule 和 ReactiveFormsModule。
But i keep getting the error.但我不断收到错误。
I want to use ngValue
instead value
because i want to bind the whole object我想使用
ngValue
代替value
因为我想绑定整个对象
So i have所以我有
TS FILE文件
sendEmailSuppliersList = [
{
guid:1,
isChecked: true,
name: 'supplier 1',
contactName:'person 1',
contactEmail:'email 1'
},
{
guid:2,
isChecked: false,
name: 'supplier 2',
contactName:'person 2',
contactEmail:'email 2'
},
]
HTML HTML
<tr class="supplierList" *ngFor="let supplier of sendEmailSuppliersList; let i = index">
<td>
<input type="checkbox" class="checkbox"
[checked]="supplier.isChecked" (change)="onSendEmailSupplierCheck($event)"
[ngValue]="supplier"
/>
</td>
</tr>
How can i use ngValue on my input type checkbox ?如何在我的输入类型复选框上使用 ngValue?
If i use [value]
then i don't get any error, but with [value]
i can't bind my whole object...如果我使用
[value]
那么我不会收到任何错误,但是使用[value]
我无法绑定我的整个对象...
ngValue
is a property of the NgSelectOption
directive, which is applied to <option>
elements. ngValue
是NgSelectOption
指令的一个属性,它应用于<option>
元素。 That means it's only defined for <option>
elements, not <input>
:这意味着它只为
<option>
元素定义,而不是<input>
:
https://angular.io/api/forms/NgSelectOption . https://angular.io/api/forms/NgSelectOption 。
That being said, as some pointed out in the comments, it doesn't make much sense to bind a whole object to a checkbox control.话虽如此,正如某些人在评论中指出的那样,将整个对象绑定到复选框控件没有多大意义。
I imagine you only can check one checkbox at time.我想您一次只能选中一个复选框。 If this is true, you can has a variable
如果这是真的,你可以有一个变量
value:any
And don't use "bannana" notation of ngModel else [ngModel] (ngModelChange) in this way并且不要以这种方式使用 ngModel else [ngModel] (ngModelChange) 的“bannana”符号
<tr class="supplierList" *ngFor="let supplier of sendEmailSuppliersList" >
<td>
<input type="checkbox" class="checkbox"
<!--the checkbox is selected only if value<>null and value.guid is supplier.guid-->
[ngModel]="value && supplier.guid==value.guid"
<!-when change, if checked, value is equal to supplier
if not checked value is equal null-->
(ngModelChange)="value=$event?supplier:null"
>
</td>
</tr>
Another option is use "radio buttons", not check-box另一种选择是使用“单选按钮”,而不是复选框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.