繁体   English   中英

无法读取未定义的属性(读取“getAttribute”)mat-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.

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