【发布时间】:2016-11-30 08:46:02
【问题描述】:
我正在尝试使用 Angular 2 Forms 进行验证,但是当我尝试添加多个控件时。似乎它只是被忽略了。我遵循了许多不同的指南,看看其他人是如何做到的,但这些方法似乎都不起作用。
我一直在做的就是在我的模板中:
<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>
然后在我的组件中我这样做:
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)]]
});
}
但我不断收到“找不到控件‘提升’”控制台错误...
任何帮助将不胜感激!
【问题讨论】:
-
你添加
REACTIVE_FORM_DIRECTIVES到组件了吗? -
是的,它在我的指令中。抱歉,我没有显示。
-
能否请您在 plunker 中重现?
-
我可以试试,这个应用有点大。可能需要我一段时间。
-
我发现
[(ngModel)]正在制造问题。
标签: html forms typescript angular