Home Set default value for select and option tag in Angular
Reply: 0

Set default value for select and option tag in Angular

user855
1#
user855 Published in July 20, 2018, 4:50 am

So I have angular code like this

<select class="form-control" ng-model="ctrl.selectedProvider">
    <option value="">Please select a provider</option>
    <option ng-repeat="provider in ctrl.providers" ng-value={{provider.id}}>
        {{provider.description}}
    </option>
</select>

The data is like this

providers = [{
     id: 1,
     description: provider1
},{
     id: 2,
     description: provider2
}]

If there's only have one element in ctrl.providers, I want to set the default value of the select tag to this provider. Otherwise, I want to keep it to "Please select a provider" and let the user select the provider he wants. I have googled that and found ng-init, but I think it's not useful for my case. Any ideas? Thanks

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311753 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO