Search code examples
angularmaterialize

How to get the text of a select in angular 7 to send to firebase?


I'm trying to get the text of the options of a select and then I have to send it to firebase database, do you got an idea of how? I'm also working iwth materialize. Thanks.

<form #alquilerForm="ngForm" (ngSubmit)="enviarDatos(alquilerForm)">
    <div class="input-field col s12 m6">
        <select [(ngModel)]="selected" id="selected" name="selected" class="icons">
            <option value="" disabled selected>Seleecione un auto</option>
            <option value="1">2016 Honda CR-V EX</option>
            <option value="2">Hyundai Accent</option>
            <option value="3">Hilux 4WD Double Cab SR5 Cruiser Ute</option>
        </select>
    </div>
    <div>
    </div>
</form>

Solution

  • To get the selected text (or value) add `

    (change)="selectchange($event)"

    ` to the select, and then get it like this:

    selectchange(sel){ 
      const value = sel.target.value; 
      const text = sel.target.options[sel.target.selectedIndex].text; 
    }