Angular.js: Setting a Default Option in Select Box
When using
Question
How can we set the first option in the data as the default value, resulting in an output like this:
<select> <option value="0" selected="selected">Something Cool</option> <option value="1">Something Else</option> </select>
Answer
Angular.js provides an easy way to achieve this using the ng-init directive:
<select ng-init="somethingHere = options[0]" ng-model="somethingHere" ng-options="option.name for option in options"> </select>
In this code, ng-init is used to initialize the somethingHere model to the first element in the options array. This ensures that the first option is selected by default.
The above is the detailed content of How to Set the First Option as Default in an Angular.js Select Box?. For more information, please follow other related articles on the PHP Chinese website!