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

问题描述 投票:0回答:1

为什么这很好用单击幻灯片切换时,I.E isPrint显示为真或假

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

但这不起作用并给出错误ERROR错误:没有值的访问器用于具有未指定的name属性的表单控件

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

我做得不好?

angular angular-material angular-directive
1个回答
1
投票

检查WORKING STACKBLITZ

因此,MatButtonToggle component doesn't implement ControlValueAccessor,你不能使用[(ngModel)]

MatButtonToggle应该是mat-button-toggle-group的一部分。

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

<mat-button-toggle 
    [checked]="isPrint" 
    (change)="isPrint = $event.source.checked">
    Toggle
</mat-button-toggle>
© www.soinside.com 2019 - 2024. All rights reserved.