角度材料动态按钮切换组表单控件访问器



按照官方演示应用程序中的示例,我编写了以下代码:

<md-button-toggle-group name="typeToggle" [(ngModel)]="chosenType" multiple>
<md-button-toggle *ngFor="let type of subTypes" [value]="type">
{{type}}
</md-button-toggle>
</md-button-toggle-group>

但是我得到错误

core.es5.js:1084 
ERROR Error: Uncaught (in promise): Error: No value accessor for form control with name: 'typeToggle'
Error: No value accessor for form control with name: 'typeToggle'

我错过了什么吗?


对评论的回应:

删除name属性会导致:

core.es5.js:1084 
ERROR Error: Uncaught (in promise): Error: No value accessor for form control with unspecified name attribute
Error: No value accessor for form control with unspecified name attribute

删除multiple消除了错误,但允许用户只选择一个。根据材料 2 文档,此语法支持选择多个。有没有另一种未记录的方法可以选择多个?我需要能够一次选择多个按钮 - 我已经看到了很多这种行为的工作演示。

我不知道为什么它坏了,但这里有一个修复。

<md-button-toggle-group name="typeToggle" [(ngModel)]="chosenType" multiple ngDefaultControl>
<md-button-toggle *ngFor="let type of subTypes" [value]="type">
{{type}}
</md-button-toggle>
</md-button-toggle-group>

为了提供一些见解,显然文档现在表明多个不支持ngModel,可以使用(click)访问值。但是,通过添加ngDefaultControl,该值是可访问的。这有点像黑客,但到目前为止它有效。

最新更新