我有一个表单,它根据下拉列表中的某些值更改其验证。如果选择了大学 A,则要求的最小百分比为 60,如果选择了大学 B,则最小百分比下降到 55。虽然我已经找到了一种方法来获取模板中的最小错误,这样我就不必费力了在我的模板中编码百分比值。虽然我不确定这是否是正确的方法。
<div formGroupName="marks">
<div class="form-group">
<label for="email" class="col-lg-3 control-label">Semester 1</label>
<div class="col-lg-3">
<input autocomplete="off"
#sem1
type="text"
min="0"
max="99"
maxlength="1"
maxlength="2"
class="form-control"
id="email"
placeholder="Sem 1 (%)"
(keypress)="onlyNumberKey($event)"
formControlName="sem1">
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('required') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Sem 1 percentage required
</span>
<span *ngIf="registrationForm.controls['marks'].controls['sem1'].hasError('min') ||
registrationForm.controls['marks'].controls['sem1'].hasError('max') ||
registrationForm.controls['marks'].controls['sem1'].hasError('minlength')||
registrationForm.controls['marks'].controls['sem1'].hasError('maxlength') &&
registrationForm.controls['marks'].controls['sem1'].touched"
class="text-danger">
Percenatge must be {{ registrationForm.get('marks.sem1')._errors.min.min }} and above.
</span>
</div>
</div>
</div>
零件
registrationForm = new FormGroup({
college: new FormControl('', [
Validators.required, dropDrownValidator
]),
marks: new FormGroup({
sem1: new FormControl('',
[
Validators.required,
Validators.min(60),
Validators.max(99),
Validators.minLength(1),
Validators.maxLength(2)
]
)
})
});