Stackblitz: https://stackblitz.com/edit/matcheckbox-checked<mat-checkbox [checked]="checked" (click)="onClick($event)">onClick me!</mat-checkbox><br/><mat-checkbox [checked]="checked" (change)="onChange($event); false">onChange me!</mat-checkbox>export class CheckboxOverviewExample { checked: boolean = false; onClick(event) { event.preventDefault(); console.log('onClick event.checked ' + event.checked); console.log('onClick event.target.checked '+event.target.checked); } onChange(event) { // can't event.preventDefault(); console.log('onChange event.checked '+event.checked); }}(click) 事件打印 undefined 值,但成功阻止了事件传播,(change) 事件打印值但会传播事件.The (click) event prints undefined values, but successfully prevents the event propagation, the (change) event prints the value but will propagate the event.相关问题:https://github.com/angular/material2/issues/1142https://github.com/angular/angular/issues/2042https://github.com/angular/material2/issues/13156推荐答案如果你想手动勾选复选框@ViewChild 获取元素的 ref 然后使用 checked 来设置值 true 或 false 像这样If you want to check the check box manually @ViewChild get the ref of the element then use checked to set the value true or false like this@ViewChild('ref') ref; onClick(bool){ this.ref._checked=bool; }示例:https://stackblitz.com/edit/matcheckbox-checked-ybru81事件处理时的相同示例:https://stackblitz.com/edit/matcheckbox-checked-a1le6oSame example while event handling: https://stackblitz.com/edit/matcheckbox-checked-a1le6o 这篇关于MatCheckbox preventDefault() 和 event.checked 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 02:53