Default select Angular

Asked

Viewed 487 times

1

 <select class="ui search dropdown" ng-model="vm.empresa" ng-options="item.id as item.label for item in vm.collectionData.items track by item.id" required >
   <option value="">Selecione uma Empresa</option>
 </select>

How do I leave the first item already selected by default?

1 answer

1


Make a ng-init to select the first element:

<select class="ui search dropdown" ng-init="vm.empresa = vm.collectionData.items[0]" ng-model="vm.empresa" ng-options="item.id as item.label for item in vm.collectionData.items track by item.id" required >
   <option value="">Selecione uma Empresa</option>
 </select>

so your model will receive the first item from the list.

  • I tested with your code, and with some variations of it but I did not get the expected result.

  • 2

    This code will only work if vm.collectionData is already loaded when the control initializes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.