How can I set as default selected option the first element of the Map I'm using for mat-select? I have:
<mat-select formControlName="formValoreScadenzario" [(value)] ="selectedValoreScadenzario">
<mat-option *ngFor="let scad of mappaValoriScadenzario" [value]="scad.key">
{{scad.value}}
</mat-option>
</mat-select>
where mappaValoriScadenzario
is my map. How can I set as default-selected value the first key-value element of my Map? I've only found examples with hardcoded value or with simple arrays of strings.
you need to fix the default selected in ngOnInit : in your .ts file, take this example -.-,
https://stackblitz.com/edit/how-to-set-default-value-of-mat-select-when-options-are-retriev
You should manually set it via setValue
method of formValoreScadenzario
FormControl after mappaValoriScadenzario
initialization (static init or getting data via HttpClient
in observable subscription) like this:
this.formValoreScadenzario.setValue(this.mappaValoriScadenzario[0].key)
UPDATE In case of FormControl
nested inside FormGroup
you can set it value like this:
this.ricercaScadenzarioForm.get('formValoreScadenzario').setValue(this.mappaValoriScadenzario[0].key)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.