【发布时间】:2019-03-13 19:20:54
【问题描述】:
【问题讨论】:
-
整个问题无需使用粗体
-
好的。 @JamesZ 感谢您的更新
-
谢谢@Vikas。我收到了你的回复
标签: angular typescript validation angular-reactive-forms
【问题讨论】:
标签: angular typescript validation angular-reactive-forms
你必须检查你的错误信息是这样的
<div *ngIf="check.errors.required && check.touched" class="e-error">
This field is required.
</div>
当您加载表单时,您正在检查 required ,那么您的字段显然是空的,所以它会抛出错误。
您将在此处获得更多信息和示例:Built In Validators 和 Reactive Form Validattions
【讨论】:
创建一个表单服务并使用 Mark FormGroup Touched 以便它默认不显示错误消息
import { FormService } from './services/form';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.scss' ]
})
export class AppComponent {
public testForm: FormGroup;
ngOnInIt(){
this.testForm.valueChanges.subscribe((data) => {
this.FormService.markFormGroupTouched(this.testForm);
})
}
}
【讨论】: