【发布时间】:2019-08-19 01:21:39
【问题描述】:
在我的 Angular 6 应用程序的注册表单中,我有一个已选中的复选框。我需要检查用户是否取消选中该复选框,如果是,我想显示一条错误消息。
这是我在 .html 文件中的复选框,
<div class="form-group">
<div class="round">
<input
type="checkbox"
id="checkbox33"
formControlName="agree">
<label for="checkbox33"></label>
</div>
<small class="text-muted">Agree to our <a href="#" class="link">Terms
Of Use</a> and <a href="#" class="link">Privacy Policy</a>.
</small>
<span *ngIf="!worldWideRegisterForm.get('agree').valid" class="text-danger">You should agree!</span>
</div>
这是我在 .ts 文件中的 ngOnInit()
ngOnInit() {
this.worldWideRegisterForm = new FormGroup({
'userName': new FormGroup({
firstName: new FormControl(null, Validators.required),
lastName: new FormControl(null, Validators.required)
}),
'email': new FormControl(null, [Validators.required, Validators.email]),
'phone': new FormControl(null, Validators.required),
'address': new FormGroup({
line1: new FormControl(null, Validators.required),
line2: new FormControl(null),
}),
'area': new FormGroup({
'province': new FormControl(null, Validators.required),
'city': new FormControl(null, Validators.required),
'postalCode': new FormControl(null, Validators.required),
}),
'password': new FormControl(null, [Validators.required, Validators.minLength(6)]),
'agree': new FormControl(true, Validators.required)
});
}
【问题讨论】:
-
如果用户无法取消选中该框,您为什么不直接禁用它?
-
谢谢@trichetriche,你是对的。但是根据要求,我不能禁用它。
标签: angular validation checkbox angular6