AngularJS | ng-selected Directive

The ng-selected Directive in AngularJS is used to specify the selected attribute of an HTML element. It can be used to select the default value specified on an HTML element. If the expression inside the ng-selected directive returns true then the selected option value will be display otherwise not display.

Syntax:

<element ng-selected="expression"> Contents... </element> 

Example: This example uses ng-selected Directive to display the selected element.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>ng-selected Directive</title>
      
    <script src=
    </script>
</head>
  
<body ng-app style="text-align:center">
      
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>ng-selected Directive</h2>     
      
    Check to select default value: 
    <input type="checkbox" ng-model="sort">
      
    <br><br>
      
    <select name="geek" >
        <option value="1" >Merge sort</option>
        <option value="2" ng-selected="sort">Quick sort</option>
        <option value="3">Bubble sort</option>
        <option value="4">Insertion sort</option>
    </select>
</body>
  
</html>                    

chevron_right


Output:
Before checked the checkbox:
ngselected
After checked the checkbox:
ngselected



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.