Angular 2 Form“找不到控件” [英] Angular 2 Form "Cannot Find Control"
问题描述
我试图使用Angular 2 Forms进行验证,但是当我尝试添加多个控件时。它似乎只是被忽略。我遵循了许多不同的指南来看看其他人是如何做到这一点的,但这些方法似乎都没有奏效。
我一直在做的是我的模板:
< form [formGroup] =formnovalidate(ngSubmit)=save(form.valid)>
< div class =rowid =message-wrapper>
< label>讯息< / label>
必须留言(最少10个字符)。
< / small>
< textarea
class =textarea-scaled
type =text
[(ngModel)] =campaign.message
formControlName =message
placeholder =这将由支持者发回,并带有返回此广告系列的网址>
< / textarea>
< / div>
< div class =rowid =promo-wrapper>
< label>促销:< / label>
促销是必需的,应该在10到100个字符之间
< / small>
class =textarea-scaled
type =text
[(ngModel)] =campaign.promotion
formControlName =promotion
placeholder =您希望通过促销信息发送什么?>
< / textarea>
< / div>
< / form>
然后在我的组件中执行此操作:
form:FormGroup;
构造函数(私有构建器:FormBuilder,
private _dataservice:DataService){
this.form = builder.group({
message: ['',[Validators.required,Validators.minLength(10)]],
promotion:['',[Validators.required,Validators.minLength(10)]]
});
}
但是我不断收到无法找到控件提升控制台错误。 ..
任何帮助将不胜感激!
不是原问题的答案,但这可能是有用的,如果你从谷歌跳到这里。
你需要检查这些东西。
-
对于所有具有
名称
属性> [ngModel] -
如果从验证中排除某些字段,则添加
[ngModelOptions] = {standalone:true}
(记住第一条规则,仍然需要一个名字) 确保你有
formControlName
属性,用于您要验证的控件。 (请记住第一条规则)
I am trying to use Angular 2 Forms for validation, but when I try to add more than one control. It seems like it just gets ignored. I have followed many different guides to see how everyone else does it, but none of those ways seem to work.
What I have been doing is this in my template:
<form [formGroup]="form" novalidate (ngSubmit)="save(form.valid)">
<div class="row" id="message-wrapper">
<label>Message</label>
<small [hidden]="form.controls.message.valid || (form.controls.message.pristine && !submitted)">
Message is required (minimum 10 characters).
</small>
<textarea
class="textarea-scaled"
type="text"
[(ngModel)]="campaign.message"
formControlName="message"
placeholder="This will be sent out by supporters with a URL back to this campaign">
</textarea>
</div>
<div class="row" id="promo-wrapper">
<label>Promotion: </label>
<small [hidden]="form.controls.promotion.valid ||(form.controls.promotion.pristine && !submitted)">
Promotion is required and should be between 10 and 100 characters
</small>
<textarea
class="textarea-scaled"
type="text"
[(ngModel)]="campaign.promotion"
formControlName="promotion"
placeholder="What would you like to be sent out in promotional messages?">
</textarea>
</div>
</form>
Then in my component I do this:
form: FormGroup;
constructor(private builder: FormBuilder,
private _dataservice: DataService) {
this.form = builder.group({
"message": ['', [Validators.required, Validators.minLength(10)]],
"promotion": ['', [Validators.required, Validators.minLength(10)]]
});
}
But I keep getting a "Cannot find control 'promotion'" console error...
Any help will be appreciated!
This may not be the answer to the original question, but this may be useful if you jumped here from google.
You need to check these things.
You must have a "
name
" attribute for all the controls which has[ngModel]
If you exclude some fields from validation, then add
[ngModelOptions]="{standalone: true}"
(remember the first rule, still you need a "name")Make sure you have
formControlName
attribute for the controls that you are going to validate. (remember the first rule)
这篇关于Angular 2 Form“找不到控件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!