[英]TypeError: Cannot read properties of undefined (reading 'includes') on clicking checkbox
[英]Cannot read properties of undefined (reading 'getAttribute') mat-checkbox
我正在嘗試使用一些 Angular 材質組件,例如 mat-checkbox,但是當我設置自定義屬性時,我得到
無法讀取未定義的屬性(讀取“getAttribute”)
使用的代碼是這樣的:
<li *ngFor="let instancia of tipo_maquina.instancias">
<mat-checkbox
(change)="onCheckChange($event, $event.source, $event.checked)"
[attr.typeid]="instancia.id">
{{instancia.nombre}}
</mat-checkbox>
</li>
onCheckChange(event: any, checkbox: MatCheckbox, isChecked: boolean){
console.log("check event")
console.log(event.target.getAttribute('typeid'));
}
$event
的類型應該是MatCheckboxChange
並且它有 2 個屬性
/** Change event object emitted by MatCheckbox. */
export declare class MatCheckboxChange
{
/** The source MatCheckbox of the event. */
source: MatCheckbox;
/** The new `checked` value of the checkbox. */
checked: boolean;
}
所以目標在MatCheckboxChange
上不存在,你會得到一個錯誤error TS2339: Property target' doesn't exist on type 'MatCheckboxChange' 。
正如@MikeOne 建議的那樣,只需將instancia.id
傳遞給onCheckChange($event, instancia.id)
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.