【问题标题】:How to throw an error if markdown editor left empty in the form?如果 Markdown 编辑器在表单中留空,如何抛出错误?
【发布时间】:2019-01-21 22:39:43
【问题描述】:
我在我的 Angular HTML 文件表单中使用了 markdown 编辑器,如下所示:
<td-text-editor class="full-width" formControlName="description"></td-text-editor>
在表单中,如果我们将描述留空,它应该会抛出一个错误。有输入吗?
【问题讨论】:
标签:
angular
markdown
teradata-covalent
【解决方案1】:
只需向您的 formControl 添加一个必需的验证器,然后您就可以在模板上显示该 formControl 的错误
this.myform = new FormGroup({
'description': new FormControl(this.hero.power, Validators.required)
});
在您的模板上
<input id="name" class="form-control"
formControlName="description" required >
<div *ngIf="description.invalid && (description.dirty || description.touched)"
class="alert alert-danger">
<div *ngIf="name.errors.required">
Description is required.
</div>