Skip to content
Related Articles

Related Articles

AngularJS | ng-selected Directive

View Discussion
Improve Article
Save Article
  • Last Updated : 06 Sep, 2022
View Discussion
Improve Article
Save Article

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 displayed otherwise not displayed. 

Syntax:

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

Parameter value:

  • expression: It is used for setting the element’s selected attribute if it returns true.

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

HTML




<!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>
    <h3>ng-selected Directive</h3>    
        
<p>Check to select default value:</p>
  
    <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>

Output:

 

Example 2: This example describes the use of the ng-selected Directive in AngularJS by disabling the checkbox & ng-selected option.

HTML




<!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>
    <h3>ng-selected Directive</h3>    
    Check to select default value:
    <input type="checkbox" disabled 
           ng-model="sort">
    <br><br>
    <select name="geek" >
        <option value="1" >Merge sort</option>
        <option value="2" 
                ng-selected="sort" disabled>
              Quick sort
          </option>
        <option value="3">Bubble sort</option>
        <option value="4">Insertion sort</option>
    </select>
</body>
</html>

Output:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!