I have a dropdown menu with a couple of options and I want the first one to be selected by default, but it doesn’t work. I suspect this is because in the .ts file I initialize the control with the undifined value by default, but even if I try to pass a value inside it still nothing happens, how can I fix that?
HTML
<select formControlName = "unit_measurement" name="unit_measurment" id="unit_measurment">
<option selected disabled value="">Выберите ед. измерения</option>
<option *ngFor="let um of unitMeasurements" [ngValue] = "um" value="">{{ um.value }}</option>
</select>
TS
addForm = new FormGroup({
name: new FormControl(undefined,[
Validators.required
]),
quantity: new FormControl(undefined,[
Validators.required,
Validators.min(0)
]),
unit_cost: new FormControl(undefined,[
Validators.required,
Validators.min(0)
]),
unit_measurement: new FormControl(undefined,[
Validators.required
])
})
I expect the first option tag to get selected by default
2
Answers
Create unit_measurements enum and add the value into the form controller
example for enum
you have to remove by default and add the default value you want
other one you can setValue for fromController in ngOnInit() example
this.workOrderForm.get('unit_measurement').setValue('Выберите ед. измерения');