简体   繁体   English

无法读取未定义的属性(读取“getAttribute”)mat-checkbox

[英]Cannot read properties of undefined (reading 'getAttribute') mat-checkbox

I'm trying to use some Angular Material Components such as mat-checkbox, but when I set a custom attribute, I get我正在尝试使用一些 Angular 材质组件,例如 mat-checkbox,但是当我设置自定义属性时,我得到

Cannot read properties of undefined (reading 'getAttribute')无法读取未定义的属性(读取“getAttribute”)

The code used is as it goes:使用的代码是这样的:

<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'));
}

The type of $event should be MatCheckboxChange and it has 2 properties $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;
    }

So target does not exist on MatCheckboxChange and you would get an error error TS2339: Property target' does not exist on type 'MatCheckboxChange' .所以目标在MatCheckboxChange上不存在,你会得到一个错误error TS2339: Property target' doesn't exist on type 'MatCheckboxChange'

As @MikeOne suggested, just pass instancia.id to the onCheckChange($event, instancia.id) method.正如@MikeOne 建议的那样,只需将instancia.id传递给onCheckChange($event, instancia.id)方法。

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

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