angular - Angular - 为表单控件中的输入类型编号设置默认值
问题描述
我正在尝试将输入类型编号的默认(初始)值设置为 1,但很难弄清楚如何去做。我知道value="1"
如果没有,我可以设置值formControlName
。我试过了inputNum: new FormControl(1, [Validators.required, Validators.pattern('^[1-9]\\d*$')])
和
inputNum: new FormControl([1, Validators.required, Validators.pattern('^[1-9]\\d*$')])
没有成功。
输入的初始值应该是 1 但我应该能够将它设置为 1 到任何正整数。
<input formControlName="inputNum" class="form-control" [ngClass]="{'is-invalid':inputNumForm.touched && inputNumForm.errors}" type="number">
<div class="invalid-feedback" *ngIf="inputNumForm.touched && inputNumForm.errors">
<label *ngIf="inputNumForm.errors.required">Numbers Only</label>
</div>
public form = new FormGroup({
inputNum: new FormControl('', [Validators.required, Validators.pattern('^[1-9]\\d*$')]),
});
get inputNumForm(): FormControl {
return this.form.controls['inputNum'] as FormControl;
}
解决方案
public form = new FormGroup({
inputNum: new FormControl('1', [Validators.required, Validators.pattern('^[1-9]\\d*$')]),
});
推荐阅读
- google-apps-script - 将 textarea HTML 输入中的值插入到一列谷歌表格中
- jenkins - Jenkins获取环境变量形式调用shell脚本
- r - 使用 if else 语句根据四列对数据进行子集化
- java - XmlHttpRequest 请求的资源上不存在“Access-Control-Allow-Origin”标头 - Chrome 浏览器
- c++ - 当我迭代它时,如何获得指向 unordered_map 中键的指针?
- haskell - Rigid / skolem 类型变量:作为参数很好,但使用本地 where/let 语句转义范围
- mysql - Liquibase:如何更新变更日志?
- python - 如何将原始代码转换为函数示例
- c# - CEFSharp 应用程序显示空白屏幕,然后在一分钟后自行响应
- android - android protobuf javalite 和 R8