如何在formControl中设置默认值?
how to set default value in formControl?
我正在尝试在输入字段中设置时间的默认值,但无法使用“值”属性来完成。
<input class="form-control" name="absolute type="time" value="23:59" formControlName="absoluteTime">
我试过使用
<div [formGroup]="dueDateControl" class="row">
<input class="form-control style__inputText style__inputText-forms duration" name='absoluteTime' [readonly]="context === 'details'" formControlName="absoluteTime" type="time" required>
</div>
.ts 文件:
ngOnInit() {
this.dueDateControl.addControl('absoluteTime', new FormControl('23:59', Validators.required));
}
什么都没有显示,我仍然得到 --:-- !没有默认值!
请这样使用
<div [formGroup]="dueDateControl" class="row">
<input class="form-control style__inputText style__inputText-forms duration" name='absoluteTime' formControlName="absoluteTime" type="time" required>
</div>
dueDateControl= this.formBuilder.group({
absoluteTime: [''],
});
constructor(private formBuilder: FormBuilder) {}
ngOnInit(): void {
this.dueDateControl.get('absoluteTime')?.setValue('19:20')
}
Ts File
newDayDuration: 0;
<div class="col s4">
<mat-form-field appearance="outline">
<mat-label>{{ 'LABEL_DAYS_NEW' | translate: lang }}</mat-label>
<input (keypress)="numberOnly($event)" placeholder="{{'LABEL_TOTAL_DAY_PLACEHOLDER' | translate:lang}}" [maxLength]="vendorRules.DAYS_ENTER_MAX_LENGTH" matInput [value]="newDayDuration" [formControlName]="'newDayDuration'">
</mat-form-field>
</div>
我正在尝试在输入字段中设置时间的默认值,但无法使用“值”属性来完成。
<input class="form-control" name="absolute type="time" value="23:59" formControlName="absoluteTime">
我试过使用
<div [formGroup]="dueDateControl" class="row">
<input class="form-control style__inputText style__inputText-forms duration" name='absoluteTime' [readonly]="context === 'details'" formControlName="absoluteTime" type="time" required>
</div>
.ts 文件:
ngOnInit() {
this.dueDateControl.addControl('absoluteTime', new FormControl('23:59', Validators.required));
}
什么都没有显示,我仍然得到 --:-- !没有默认值!
请这样使用
<div [formGroup]="dueDateControl" class="row">
<input class="form-control style__inputText style__inputText-forms duration" name='absoluteTime' formControlName="absoluteTime" type="time" required>
</div>
dueDateControl= this.formBuilder.group({
absoluteTime: [''],
});
constructor(private formBuilder: FormBuilder) {}
ngOnInit(): void {
this.dueDateControl.get('absoluteTime')?.setValue('19:20')
}
Ts File
newDayDuration: 0;
<div class="col s4">
<mat-form-field appearance="outline">
<mat-label>{{ 'LABEL_DAYS_NEW' | translate: lang }}</mat-label>
<input (keypress)="numberOnly($event)" placeholder="{{'LABEL_TOTAL_DAY_PLACEHOLDER' | translate:lang}}" [maxLength]="vendorRules.DAYS_ENTER_MAX_LENGTH" matInput [value]="newDayDuration" [formControlName]="'newDayDuration'">
</mat-form-field>
</div>