绑定 Mat-Button-Toggle v 绑定 Mat-Slide-Toggle

Binding Mat-Button-Toggle v binding Mat-Slide-Toggle

这怎么能正常工作。 I.E isPrint 在单击幻灯片切换时显示为 true 或 false

<div>
 <mat-slide-toggle [(ngModel)]="isPrint" #toggleSlide></mat-slide- 
 toggle>
isPrint: {{ isPrint }}
</div>

但这不起作用并给出错误 ERROR Error: No value accessor for form control with an unspecified name attribute

<div>
  <mat-button-toggle [(ngModel)]="isPrint" #toggleBtn>Toggle</mat-button-toggle>
  isPrint: {{ isPrint }}
</div>

我做错了什么?

检查 WORKING STACKBLITZ

, 因此,您不能在其上使用 [(ngModel)]

MatButtonToggle 应该是 mat-button-toggle-group.

的一部分

但是如果你想把它作为一个独立的组件使用并绑定模型,你必须做如下的事情:~

<mat-button-toggle 
    [checked]="isPrint" 
    (change)="isPrint = $event.source.checked">
    Toggle
</mat-button-toggle>