【发布时间】:2018-10-11 17:35:20
【问题描述】:
在使用 Angular 5.2.5 材质的 Angular 5 应用中,使用此代码时,默认选中的单选按钮正常工作,但控制台中出现错误
代码:
<mat-radio-group [(ngModel)]="collection">
<mat-radio-button class="collectionRadioButton"
*ngFor="let coll of collections"
[value]="coll.endPoint"
[checked]="coll.isDefault"
(change)="clearResults()">
{{coll.label}}
</mat-radio-button>
</mat-radio-group>
Chrome 控制台出错:
ERROR Error: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.
Example 1: <input [(ngModel)]="person.firstName" name="first">
Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">
at Function.TemplateDrivenErrors.missingNameException (forms.js:5891)
at NgModel._checkName (forms.js:6244)
at NgModel._checkForErrors (forms.js:6217)
at NgModel.ngOnChanges (forms.js:6099)
at checkAndUpdateDirectiveInline (core.js:12407)
at checkAndUpdateNodeInline (core.js:13935)
at checkAndUpdateNode (core.js:13878)
at debugCheckAndUpdateNode (core.js:14771)
at debugCheckDirectivesFn (core.js:14712)
但是当给 mat-radio-group 添加名字时
<mat-radio-group [(ngModel)]="collection" [name]="'aList'">
那么单选按钮列表仍然正确生成,但默认选中的不再起作用...
【问题讨论】:
-
完整代码?看起来您的代码嵌套在
<form>
标签: angular forms angular-material angular5 radio-group