简体   繁体   English

Angular mat-checkbox getElementById

[英]Angular mat-checkbox getElementById

I've got a problem with the mat-checkbox from angular material.我对来自 angular 材料的 mat-checkbox 有疑问。 I've given the checkbox an ID like this:我给了复选框一个像这样的 ID:

<mat-checkbox class="toolbar-checkbox" id="myCheckbox">
    MyCheckbox
</mat-checkbox>

After that I'm trying to something with this element like this:之后,我尝试使用这样的元素:

(<MatCheckbox>document.getElementById('myCheckbox')).checked = true;

But unfortunally I'm getting this error:但不幸的是我收到了这个错误:

TS2352: Type 'HTMLElement' cannot be converted to type 'MatCheckbox'. 
Property '_changeDetectorRef' is missing in type 'HTMLElement'. 
(<MatCheckbox>document.getElementById('asdasd')).checked = true;

How can I solve this, or is there a better way to do something with an checkbox without using [(ngModel)]?我该如何解决这个问题,或者有没有更好的方法可以在不使用 [(ngModel)] 的情况下使用复选框来做某事?

Use ViewChild decorator. 使用ViewChild装饰器。 Change your template to this: 将模板更改为此:

<mat-checkbox class="toolbar-checkbox" #myCheckbox>
    MyCheckbox
</mat-checkbox>

.. and in your typescript: ..并在您的打字稿中:

import { ViewChild } from '@angular/core';
import { MatCheckbox } from '@angular/material';

@Component({
    ..... 
})
export class YourComponent {
    @ViewChild('myCheckbox') private myCheckbox: MatCheckbox;

    // You can then access properties of myCheckbox 
    // e.g. this.myCheckbox.checked
}

See this working StackBlitz demo . 请参阅此工作台StackBlitz演示

You can also do: 您也可以:

<mat-checkbox class="toolbar-checkbox" [checked] = "myCondition" id="myCheckbox">
    MyCheckbox
</mat-checkbox>

where myCondition is set in the template or the class and could be any logical expression. 其中myCondition是在模板或类中设置的,可以是任何逻辑表达式。

or 要么

<mat-checkbox class="toolbar-checkbox" id="myCheckbox" [checked] = "myCondition()">
    MyCheckbox
</mat-checkbox>

where myCondition() is a method defined in the class and should return a boolean value, based on any logical expression. 其中myCondition()是在类中定义的方法,并且应基于任何逻辑表达式返回布尔值。

This would allow you skip any DOM manipulation and take care of the checkbox checked status in the template. 这将允许您跳过任何DOM操作,并注意模板中复选框已选中的状态。

Digging through what mat-checkbox generate in html i found that mat-checkbox create a hidden html input type="checkbox" so you can get the checked value by adding "-input" in the id in the getElementById call, for example if your mat-checkbox id is myinputid :通过挖掘 html 中生成的mat-checkbox ,我发现mat-checkbox创建了一个隐藏的 html input type="checkbox"因此您可以通过在getElementById调用的 id 中添加“-input”来获取选中的值,例如,如果您mat-checkbox id 是myinputid

const getmyinput: boolean = (<HTMLInputElement>document.getElementById('myinputid-input')).checked;

It solve the problem for dynamic id , example adding string i in the middle:它解决了动态 id的问题,例如在中间添加字符串i

const getmyinput: boolean = (<HTMLInputElement>document.getElementById('myinputid' + String(i) + '-input')).checked;

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

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