AngularJS Select

In AngularJS, you can create a dropdown list (select box) based on items in an array, or an object.

Using ng-options

You should use the ng-option directive to create a dropdown list, based on an object or an array in AngularJS.

See this example:

<!DOCTYPE html>  

<html>  

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  

<body>  

<div ng-app="myApp" ng-controller="myCtrl">  

<select ng-model="selectedName" ng-options="x for x in names">  

</select>  

</div>  

<script>  

var app = angular.module('myApp', []);  

app.controller('myCtrl', function($scope) {  

    $scope.names = ["Java", "PHP", ".Net", "AngularJS", "C/C++"];  

});  

</script>  

</body>  

</html>

Note:

You can also use the ng-repeat directive to make the same dropdown list as ng-options.

See this example:

<!DOCTYPE html>  

<html>  

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  

<body>  

<div ng-app="myApp" ng-controller="myCtrl">  

<select>  

<option ng-repeat="x in names">{{x}}</option>  

</select>  

</div>  

<script>  

var app = angular.module('myApp', []);  

app.controller('myCtrl', function($scope) {  

        $scope.names = ["Java", "PHP", ".Net", "AngularJS", "C/C++"];  

});  

</script>  

<p>The same example of dropdown list using the ng-repeat directive.</p>  

</body>  

</html>

ng-options vs. ng-repeat

Although, both can be used for dropdown list, but ng-repeat directive repeats a block of HTML code for each item in an array, it can be used to create options in a dropdown list, while the ng-options directive was made especially for filling a dropdown list with options, and has at least one important advantage:

Dropdowns made with ng-options allows the selected value to be an object, while dropdowns made from ng-repeat has to be a string.

Consider that you have an array of objects:

$scope.cars = [  

    {model : "Ford Mustang", color : "red"},  

    {model : "Fiat 500", color : "white"},  

    {model : "Volvo XC90", color : "black"}  

];  

    Limitation of ng-repeat

    The ng-repeat directive has a limitation that the selected value must be a string:

    See this example:

    <!DOCTYPE html>  
    
    <html>  
    
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    
    <body>  
    
    <div ng-app="myApp" ng-controller="myCtrl">  
    
    <p>Select a car:</p>  
    
    <select ng-model="selectedCar">  
    
    <option ng-repeat="x in cars" value="{{x.model}}">{{x.model}}</option>  
    
    </select>  
    
    <h1>You selected: {{selectedCar}}</h1>  
    
    </div>  
    
    <script>  
    
    var app = angular.module('myApp', []);  
    
    app.controller('myCtrl', function($scope) {  
    
        $scope.cars = [  
    
            {model : "Ford", color : "red"},  
    
            {model : "Honda", color : "white"},  
    
            {model : "Volvo", color : "black"},  
    
            {model : "Hundai", color : "gray"}  
    
        ];  
    
    });  
    
    </script>  
    
    </body>  
    
    </html>

    While using the ng-options directive, you can select an object value:

    See this example:

    <!DOCTYPE html>  
    
    <html>  
    
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
    
    <body>  
    
    <div ng-app="myApp" ng-controller="myCtrl">  
    
    <p>Select a car:</p>  
    
    <select ng-model="selectedCar" ng-options="x.model for x in cars">  
    
    </select>  
    
    <h1>You selected: {{selectedCar.model}}</h1>  
    
    <p>It's color is: {{selectedCar.color}}</p>  
    
    </div>  
    
    <script>  
    
    var app = angular.module('myApp', []);  
    
    app.controller('myCtrl', function($scope) {  
    
           $scope.cars = [  
    
            {model : "Ford", color : "red"},  
    
            {model : "Honda", color : "white"},  
    
            {model : "Volvo", color : "black"},  
    
            {model : "Hundai", color : "gray"}  
    
        ];  
    
      
    
    });  
    
    </script>  
    
    </body>  
    
    </html>

    Use data source as an object

    We can also use data source as an object.

    Consider that you have an object with following key-value pairs:

    $scope.cars = {  
    
            car01 : "Ford",  
    
            car02 : "Honda",  
    
            car03 : "Volvo",  
    
            car03 : "Hundai",  
    
        }; 

      The expression in the ng-options attribute is a bit different for objects:

      See this example:

      <!DOCTYPE html>  
      
      <html>  
      
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
      
      <body>  
      
      <div ng-app="myApp" ng-controller="myCtrl">  
      
      <p>Select a car:</p>  
      
      <select ng-model="selectedCar" ng-options="x for (x, y) in cars">  
      
      </select>  
      
      <h1>You selected: {{selectedCar}}</h1>  
      
      </div>  
      
      <script>  
      
      var app = angular.module('myApp', []);  
      
      app.controller('myCtrl', function($scope) {  
      
          $scope.cars = {  
      
              car01 : "Ford",  
      
              car02 : "Honda",  
      
              car03 : "Volvo",  
      
              car03 : "Hundai",  
      
          }  
      
      });  
      
      </script>  
      
      </body>  
      
      </html>

      Example2:

      The selected value will always be the value in a key-value pair.

      The value in a key-value pair can also be an object:

      $scope.cars = {  
      
      car01 : {brand : "Ford", model : "Mustang", color : "red"},  
      
      car02 : {brand : "Honda", model : "city", color : "white"},  
      
      car03 : {brand : "Volvo", model : "XC90", color : "black"},  
      
      car04 : {brand : "Hundai", model : "Creta", color : "gray"}  
      
      }; 

        See this example:

        <!DOCTYPE html>  
        
        <html>  
        
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>  
        
        <body>  
        
        <div ng-app="myApp" ng-controller="myCtrl">  
        
        <p>Select a car:</p>  
        
        <select ng-model="selectedCar" ng-options="x for (x, y) in cars">  
        
        </select>  
        
        <h1>You selected: {{selectedCar.brand}}</h1>  
        
        <h2>Model: {{selectedCar.model}}</h2>  
        
        <h3>Color: {{selectedCar.color}}</h3>  
        
        </div>  
        
        <script>  
        
        var app = angular.module('myApp', []);  
        
        app.controller('myCtrl', function($scope) {  
        
            $scope.cars = {  
        
        car01 : {brand : "Ford", model : "Mustang", color : "red"},  
        
        car02 : {brand : "Honda", model : "city", color : "white"},  
        
        car03 : {brand : "Volvo", model : "XC90", color : "black"},  
        
        car04 : {brand : "Hundai", model : "Creta", color : "gray"}  
        
            }  
        
        });  
        
        </script>  
        
        </body>  
        
        </html>

        Comments

        Leave a Reply

        Your email address will not be published. Required fields are marked *