Search code examples
angularjsionic-frameworkng-optionsangularjs-ng-options

How to conditionally set selected property in AngularJS


I have a select-field like this:

<select ng-init="exchangeOption = widget.content.exchange[0]" 
    ng-Model="exchangeOption" 
    ng-options="exchangeOption.title for exchangeOption in widget.content.exchange"
    ng-change="internalLink(exchangeOption.url)">
</select>

As you see, right now I always select the first option. But actually the "exchanges" have a "selected"-field, which is a boolean, set to true if that option should be the selected one.

How would you do this?

Thanks in advance!


Solution

  • You can write a method to return selected exchange option by filtering the exchanges.

    <select ng-init="exchangeOption = getSelectedExchange()" 
     ng-Model="exchangeOption" ng-options="exchangeOption.title for exchangeOption in widget.content.exchange"
     ng-change= "internalLink(exchangeOption.url)"> </select>