简体   繁体   中英

Angularjs using ng-options in select with a default value

I would like to have the first element of my ng-options to be the default one:

<select data-ng-options='item.pk for item in items' data-ng-click='update()'></select>

(so this is different than having an <option value=''>Default</option> at the end of the select. I also would like update() to be called at the beginning (on the default option). How can I achieve this ?

First you want to create a model for the select element:

<select ng-model="selectedItem" ng-options="item.pk for item in items"
     ng-click="update()">
</select>

After that, in your controller, you want to set selectedItem to the first element in items , and call your update function:

$scope.selectedItem = $scope.items[0]; $scope.update();

Notice that you should define update before calling it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM