Select default option not working with Ionic Framework

懵懂的女人 提交于 2020-01-14 08:43:47

问题


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>

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

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

Why is this happening?


回答1:


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>



回答2:


Set ng-selected="true".

Example:

<option ng-selected="true">Carta di credito</option>



回答3:


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.



来源:https://stackoverflow.com/questions/25722578/select-default-option-not-working-with-ionic-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!