我已成功将“quantityControl”formControlName 中输入字段中的值实现为不小于 1。然而我的问题是在 formArray 上时。我怎样才能将其设置为不小于0或不应为负数?
这是下面的代码和我的 stackblitz 的链接代码链接 https://stackblitz.com/edit/set-quantity-not-less-than-1?file=app%2Fapp.component.ts
this.inquiryForm.get('quantityControl').valueChanges.pipe(
filter(quantity => quantity < 1)
).subscribe(value => {
console.log(value);
this.inquiryForm.get('quantityControl').setValue(1);
});
为了更好地理解表格检查这里 https://medium.com/aviabird/complete-angular2-guide-reactive-forms-in-depth-part-1-21a8e2428904.
使用 compose() 来配置具有多个自定义验证的输入字段。
this.form = formBuilder.group({
formControlNameValue:['', Validators.compose([Validators.required, positiveVal ])
]});
并实施正值
static positiveVal(control:Control):{ [key: string]: any; } {
if (Number(control.value) < 0) {
return {nonZero: true};
} else {
return null;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)