typescript 没有名称的表单控件的值访问器:''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50133800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No value accessor for form control with name: ''
提问by Adeel
ERROR Error: No value accessor for form control with name: 'sNo'
错误错误:没有名称的表单控件的值访问器:'sNo'
it is not finding formCOntrolName in my DOM
它没有在我的 DOM 中找到 formCONtrolName
component side
组件端
this.form = new FormGroup({
'sNo': new FormControl,
'question': new FormControl,
'options': new FormGroup({
'op': new FormControl,
})
});
HTML side
HTML端
<form [formGroup]="form">
<div *ngFor="let data of currentQuestionsValue">
<div *ngFor="let d of data.items ">
<strong formControlName="sNo"> {{ d.sno }}). </strong>
<span formControlName="question">{{ d.question}}</span>
<div>
<form formGroupName="options">
<label *ngFor="let key of objectKeys(d.options)" formControlName="op">
<input type="radio" name="option" [value]="d.options[key]">
<span>{{ d.options[key] }}</span>
</label>
</form>
</div>
</div>
</div>
</form>
回答by Oscar Paz
You can't apply a formControlName
directive to an strong
tag. It must be applied on a form field tag (select
, input
, textarea
) or a custom component that implements the CustomValueAccessor
interface.
您不能将formControlName
指令应用于strong
标签。它必须应用于表单字段标记(select
、input
、textarea
)或实现该CustomValueAccessor
接口的自定义组件。
回答by Sajeetharan
You should use formControlName="sNo"
on an input and not on a strong
您应该formControlName="sNo"
在输入上使用而不是在输入上使用strong