Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select default option not working with Ionic Framework

I am using Ionic Framework.
I would like to enter the first option as the default, but doing it this way does not work.
What can I do?

<select ng-model="newSpesaAereo.valuta">
    <option ng-selected="selected">Euro</option>
    <option>Dollaro</option>
    <option>Dollaro canadese</option>
    <option>Sterlina</option>
</select>

enter image description here

If I analyze the page with Google Chrome Developer Utility I watch this.

enter image description here

If I edit this HTML and I delete the selected row, then Euro is visible.

Why is this happening?

like image 573
Marco Avatar asked Sep 08 '14 10:09

Marco


People also ask

How do you get the selected option value in ionic?

Single Selection By default, the select allows the user to select only one option. The alert interface presents users with a radio button styled list of options. The action sheet interface can only be used with a single value select. The select component's value receives the value of the selected option's value.

How do you use Select in ionic?

A select should be used with child <ion-select-option> elements. If the child option is not given a value attribute then its text will be used as the value. If value is set on the <ion-select> , the selected option will be chosen based on that value.

How do I reset my ion-select?

You cannot use ion-option to reset ion-select form field. But you can provide clear button to reset ion-select if an ion-option is selected as below.


3 Answers

Set ng-selected="true".

Example:

<option ng-selected="true">Carta di credito</option>
like image 86
David living in South Korea Avatar answered Nov 07 '22 04:11

David living in South Korea


The reason the default option isn't working is because, in your controller, you've likely initialised your form model like so:

$scope.newSpesaAereo = {}

If you want to set default form options, you should also set them in the form model. For example, set 'valuta' to 'Euro' like so:

 $scope.newSpesaAereo = { valuta: "Euro" }

I hope this helps!

P.S when posting issues like this you should also include the code for the controller that the view is bound to.

like image 28
lisimba.gilkes Avatar answered Nov 07 '22 05:11

lisimba.gilkes


You need to use ng-selected instead of just selected.

https://docs.angularjs.org/api/ng/directive/ngSelected

<select ng-model="newSpesaAereo.metodoPagamento">
  <option ng-selected="selected">Carta di credito</option>
  <option>Bancomat</option>
  <option>Contanti</option>
  <option>Altro</option>
</select>
like image 27
Jamie Sutherland Avatar answered Nov 07 '22 05:11

Jamie Sutherland